15

Dieser Code erstellt eine Benachrichtigung. Wenn Sie darauf klicken, wird die aktuelle Anwendung läuft (die Absicht, in Entry, geschaffen, der meine ist nur Activity), eine leicht modifizierte Version eines Android Developers Blog:Android-Anrufmethode bei Benachrichtigung klicken

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); 
} 

Aber ich will nicht starten jede Aktivität, sondern lediglich eine Methode in der aktuellen Aktivität ausführen. Von dem, was ich bis jetzt gelesen habe, muss ich Methoden wie startActivityForResult() verwenden, intent-filters verwenden und onActivityResult() implementieren, aber nachdem ich mit all diesen Dingen rumgespielt habe, Dinge in den Intent und PendingIntent ändern, habe ich immer noch kein brauchbares Ergebnis. Ist es möglich, nur eine Methode in Entry anrufen (meine Haupt Activity, in denen die Intent erstellt wird), oder fangen alle ausgehenden oder eingehenden Intents, wenn ich auf meine neu erstellte Notification klicke?

PS. Ich entschuldige mich, wenn das ein doppelter Thread ist, SO ist im Moment ziemlich langsam, ich kann nicht richtig suchen.

Antwort

13

hinzufügen android:launchMode="singleTop" in Ihrem activity in Ihrer Manifest-Datei, haben die Methode protected void onNewIntent(Intent intent) { ... } und verwenden Sie diesen Code:

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

Works, danke. – stealthjong

+1

Beste Lösung Google gab mir, danke. –

+0

Wie bereits erwähnt vergessen Sie nicht, android hinzuzufügen: 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

hier Notificationintent ist eine andere Aktivität, und messagevalue ist eine Zeichenkette. –

4

Das funktionierte 100% für mich:

Platzieren Sie diesen Code in einem Verfahren :

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); 
    } 

Dann in der onCreate/co nstructor Ihrer Klasse tun Sie dies:

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 :) 
     } 

    } 
Verwandte Themen