15

此代碼創建通知。如果你點擊它,目前的應用程序運行(目的是Entry創建的,這是我唯一的Activity),一個Android開發者博客的稍作修改的版本:通知點擊的Android通話方法

private void makeIntent() { 
    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification note = new Notification(R.drawable.prev, "Status message!", System.currentTimeMillis()); 
    Intent intent = new Intent(this, Entry.class); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
    note.setLatestEventInfo(this, "New Email", "Unread Conversation", pi); 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 
    mgr.notify(NOTIFY_ME_ID, note); 
} 

但我不希望啓動任何活動,但只是在當前活動中運行一種方法。從我目前閱讀的內容來看,我想我必須使用像startActivityForResult()這樣的方法,使用intent-filters並執行onActivityResult(),但是在完成所有這些事情之後,改變了IntentPendingIntent中的內容,但我仍然沒有可用的結果。是否有可能以某種方式調用Entry(我的主要Activity,其中創建了Intent)中的方法,或者在單擊我新建的Notification時捕獲任何傳出或傳入的Intents

PS。我很抱歉,如果這是一個重複的線程,SO現在很慢,我無法正確搜索。

回答

13

activity添加android:launchMode="singleTop"在您的清單文件,有方法protected void onNewIntent(Intent intent) { ... },並使用此代碼:

private static final int MY_NOTIFICATION_ID = 1; 
private NotificationManager notificationManager; 
private Notification myNotification; 

void notification() { 
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    myNotification = new Notification(R.drawable.next, "Notification!", System.currentTimeMillis()); 
    Context context = getApplicationContext(); 
    String notificationTitle = "Exercise of Notification!"; 
    String notificationText = "http://android-er.blogspot.com/"; 
    Intent myIntent = new Intent(this, YourActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(YourActivity.this, 0, myIntent, Intent.FILL_IN_ACTION); 
    myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 
    myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent); 
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification); 
} 
+3

作品,謝謝。 – stealthjong

+1

Google給了我最好的解決方案,謝謝。 – 2014-06-27 19:54:24

+0

如上所述不要忘記添加android:launchMode =「singleTop」 –

0
Intent intent = new Intent(this, Notificationintent.class); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 



    Notification noti = new Notification.Builder(this) 
    .setContentTitle("APP NOTIFICATION") 
    .setContentText(messageValue) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setStyle(new Notification.BigTextStyle() 
    .bigText(messageValue)) 
    .setContentIntent(pIntent).build(); 

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
// hide the notification after its selected 
noti.flags |= Notification.FLAG_AUTO_CANCEL; 

notificationManager.notify(0, noti); 
+0

此處Notificationintent是另一個活動,而messagevalue是一個字符串。 –

4

這個工作100%對我來說:

將此代碼放在一個方法:

Intent intent = new Intent(this, YourClass.class); 
    intent.putExtra("NotiClick",true); 
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
     Notification Noti; 
     Noti = new Notification.Builder(this) 
       .setContentTitle("YourTitle") 
       .setContentText("YourDescription") 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentIntent(pIntent) 
       .setAutoCancel(true).build(); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     notificationManager.notify(0, Noti); 
    } 

然後在onCreate/co您的班級管理人員這樣做:

if (savedInstanceState == null) { 
     Bundle extras = getIntent().getExtras(); 
     if(extras == null) 
     { 
      //Cry about not being clicked on 
     } 
     else if (extras.getBoolean("NotiClick")) 
     { 
      //Do your stuff here mate :) 
     } 

    }