2014-04-02 67 views
1

在我的通知中,我想在點擊待處理意圖時啓動當前活動。在谷歌,找到很多方法,我嘗試了很多次,但沒有奏效。這裏是我的代碼,通知無法執行當前活動

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

    String startTime = intent.getStringExtra("Strar_time"); 

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     long when = System.currentTimeMillis();   // notification time 
     Notification notification = new Notification(R.drawable.ic_launcher, "reminder", when); 
     notification.defaults |= Notification.DEFAULT_SOUND; 
     notification.flags |= notification.FLAG_AUTO_CANCEL; 
     notification.vibrate = new long[]{100, 200, 100, 500}; 
     notification.defaults = Notification.DEFAULT_ALL; 

     Intent notificationIntent = new Intent(this, Goo.class); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP/* | Intent.FLAG_ACTIVITY_SINGLE_TOP*/); 

     PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent , 0); 
     notification.setLatestEventInfo(getApplicationContext(), "It's about time", "Your time is "+startTime, contentIntent); 
     notification.contentIntent=contentIntent; 
     nm.notify(NOTIF_ID, notification); 
     NOTIF_ID++; 
     Log.i("NotiID",NOTIF_ID+""); 
    } 

回答

0

嘗試下面給出的這個功能,

private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.launch_icon; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 

    String title = context.getString(R.string.app_name); 

    Intent notificationIntent = null; 

    notificationIntent = new Intent(context, Main.class); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent intent = PendingIntent.getActivity(context, 0, 
      notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    // Play default notification sound 
    notification.defaults |= Notification.DEFAULT_SOUND; 

    // Vibrate if vibrate is enabled 
    notification.defaults |= Notification.DEFAULT_VIBRATE; 
    notificationManager.notify(0, notification); 

} 
0

我發現到目前爲止,只有一個工作方式是讓一個虛活動,啓動它,並在onRusume完成它()。但是,這種技術只能將應用程序放在前面,並且在關閉時不會啓動應用程序。 然後,我使用下面的解決方法將應用程序放在前面,如果它在後臺存活,則啓動它。

清單:

<application> 
. 
. 
. 
<activity 
android:name=".DummyActivity" 
/> 

. 
. 
</application> 

在代碼:

public class DummyActivity{ 

public static boolean isAppAlive; 

    @Override 
    protected void onResume() { 
     // start application if it is not alive 
     if(!isAppAlive){ 
     PackageManager pmi = getPackageManager(); 
     Intent intent = pmi.getLaunchIntentForPackage(Your applications package name);      
      if (intent != null){ 
       startActivity(intent); 
      } 

     } 
    finish(); 
    } 

在您的主要活動的onCreateMethod。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    DummyActivity.isAppAlive = true; 
} 

很明顯,當您的應用程序完全關閉時,isAppAlive將恢復爲其默認值,該值爲false。因此應用程序將開始。

相關問題