2016-03-02 58 views
0

我从gmail下载了一个文件。下载完成后,我需要能够点击通知,在我的应用程序中打开下载的文件。 我的文件类型是csv。如何打开下载的文件通知点击在Android?

enter image description here

点击上面下载的文件,它会打开通过我的app.how开?

<activity 
      android:label="@string/app_name" 
      android:name=".Fakeactivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.BROWSABLE" /> 
       <data android:scheme="file" /> 
       <data android:host="*" /> 
       <data android:mimeType="text/comma-separated-values" /> 
       <data android:pathPattern=".*\\.csv" /> 
      </intent-filter> 
     </activity> 

使用上面的代码来打开通过我app.in FakeActivty csv文件,我有写代码读取csv文件。

回答

0

有你落实通知 点击的功能,你的程序可以在这里处理打开CSV文件是用于读取csv文件 这里的代码是开放活动的代码,用于将响应作秀CSV文件

final Intent intent = new Intent(this, CSVClass.class); 
     \\ data you want to send to CSV 
    intent.putExtra("key", "value"); 
    final PendingIntent contentIntent = 
    PendingIntent.getActivity(this, 
     0, intent), 0); 
    public class CSVFile { 
    InputStream inputStream; 

    public CSVFile(InputStream inputStream){ 
    this.inputStream = inputStream; 
    } 

    public List read(){ 
    List resultList = new ArrayList(); 
    BufferedReader reader = new BufferedReader(new 
    InputStreamReader(inputStream)); 
    try { 
     String csvLine; 
     while ((csvLine = reader.readLine()) != null) { 
      String[] row = csvLine.split(","); 
      resultList.add(row); 
     } 
    } 
    catch (IOException ex) { 
     throw new RuntimeException("Error in reading CSV file: "+ex); 
    } 
    finally { 
     try { 
      inputStream.close(); 
     } 
     catch (IOException e) { 
      throw new RuntimeException("Error while closing input 
     stream: "+e); 
     } 
    } 
    return resultList; 
    } 

} 

来源http://javapapers.com/android/android-read-csv-file/

+0

阅读已在我的活动中完成。但下载后点击我的文件它不会打开。实际上,我不知道在哪里写通知onclick函数? – Rajesh

+0

你的问题是在点击通知,因为我明白你需要添加在你的代码中创建通知的地方final Intent intent = new Intent(this,MyActivity.class); intent.setData(data); intent.putExtra(“key”,“value”); final PendingIntent contentIntent = PendingIntent.getActivity(this,0,intent),0); – androidAhmed

+0

如果您希望您的应用程序从下载打开文件,您需要设置广播接收器以告诉操作系统,如果您希望应用程序从下载文件打开,您需要在意图过滤器中修改您的程序能够打开.csv文件的.csv – androidAhmed

2

使用挂起的意图相同。这里是一个示例代码。我希望它有帮助

Intent intent = new Intent(); 
    intent.setAction(android.content.Intent.ACTION_VIEW); 
    File file = new File("YOUR_FILE_URI"); // set your filepath 
    intent.setDataAndType(Uri.fromFile(file), "YOUR_FILE_TYPE/*"); 

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    Notification noti = new NotificationCompat.Builder(this) 
      .setContentTitle("Download completed") 
      .setContentText("File name") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentIntent(pIntent).build(); 

    noti.flags |= Notification.FLAG_AUTO_CANCEL; 

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(0, noti); 
+0

它适用于我 – SAurabh

+0

快乐编码..... – Coder