2011-11-01 102 views
5

我知道,這裏有很多這樣的內容,但我一直在嘗試解決方案並且一直沒有到任何地方。 谷歌文檔上的示例,以及我在這裏找到的其他5種方法都不適用於我。 就像典型的情況一樣,當我點擊通知時,它會關閉狀態欄,並且屏幕上不顯示任何新內容。 我正在從服務創建通知,並需要通知來觸發尚未創建的新活動。 我還需要一種通過意圖將信息傳遞給該活動的方法。單擊通知時從服務中啓動活動

是的......這是Android

接下來是我的代碼的破碎的殘餘的Java。

package com.bobbb.hwk2; 

import java.io.FileOutputStream; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.ContentResolver; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.IBinder; 
import android.provider.ContactsContract; 
import android.widget.Toast; 

public class contactBackup extends Service 
{ 
    private NotificationManager nManager; 
    private static final int NOTIFY_ID = 1100; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 

     String ns = Context.NOTIFICATION_SERVICE; 
     nManager = (NotificationManager)getSystemService(ns); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 

     // inform user that service has started 
     Toast.makeText(getApplicationContext(), R.string.service_started,Toast.LENGTH_LONG).show(); 

     String data = lookUpContacts(); 
     if(saveToSDCard(getResources().getString(R.string.backup_file_name),data)) 
     { 
      Context context = getApplicationContext(); 

      // create the statusbar notification 
      Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN); 
      nIntent.setClass(context,contactViewer.class); 
      //nIntent.putExtra("data",data); 


      Notification msg = new Notification(R.drawable.icon,"All contacts records have been written to the file.",System.currentTimeMillis()); 
      // start notification 
      //PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),0,nIntent,PendingIntent.FLAG_UPDATE_CURRENT|Intent.FLAG_FROM_BACKGROUND); 
      PendingIntent pIntent = PendingIntent.getActivity(this,0,nIntent,0); 
      msg.flags = Notification.FLAG_AUTO_CANCEL; 
      msg.setLatestEventInfo(context, 
            "success", 
            "All contacts records have been written to the file.", 
            pIntent); 
      nManager.notify(NOTIFY_ID,msg); 


     } 



    } 

    @Override 
    public void onDestroy() 
    { 
     nManager.cancel(NOTIFY_ID); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) 
    { 
     return null; 
    } 

    // function returns string containing information 
    // from contacts 
    public String lookUpContacts() 
    { 
     ... 
    } 

    public boolean saveToSDCard(String fileName, String data) 
    { 
     ... 
    } 

} 

我只能希望,無論是造成我的問題是可以解決的事情,而不是更瘋狂的毛刺我已經得到與Eclipse的(這是任何一個其他人似乎見過>:U)

如果你能幫我解決這個問題,請分享。 如果你不能與此特定問題不禁覺得有義務要說一下發帖,風格,主題,或好的做法無關的東西,那就不要

謝謝:d

回答

6

編輯:

你將不得不增加一個標誌爲FLAG_ACTIVITY_NEW_TASK:

nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

這是因爲你從outsi啓動你的應用程序(從系統通知欄)。

+0

好,這不是真的如何使用putExtra ......但根據你的建議,我嘗試了addi將該標誌同意給Intent和PendingIntent並且沒有任何區別。不過謝謝。 – user980058

+0

您的編輯與我所做的匹配。仍然沒有骰子:3 – user980058

0

這是人們過度勞累時發生的事情。 XD 沒有一個我厭倦工作的教程的唯一原因是因爲我在清單中拼錯了我的活動名稱。 感謝由

停止
0

只添加contactBackup(服務類)以下,

Intent nIntent = new Intent(this,contactViewer.class);//Intent nIntent = new Intent(Intent.ACTION_MAIN); 
     nIntent.setClass(context,contactViewer.class); 
     nIntent.putExtra("data",data); 

    nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     Notification msg = new Notification(R.drawable.icon,"All contacts records have been written to the file.",System.currentTimeMillis()); 
     // start notification 
     //PendingIntent pIntent = PendingIntent.getService(getApplicationContext(),0,nIntent,PendingIntent.FLAG_UPDATE_CURRENT|Intent.FLAG_FROM_BACKGROUND); 
     PendingIntent pIntent = PendingIntent.getActivity(this,0,nIntent,0); 
     msg.flags = Notification.FLAG_AUTO_CANCEL; 
     msg.setLatestEventInfo(context, 
           "success", 
           "All contacts records have been written to the file.", 
           pIntent); 
     nManager.notify(NOTIFY_ID,msg); 

然後獲得contactViewer類值,

如,

String s=getIntent().getStringExtra("data");