在通知点击通话的Android方法(Android call method on notificat

2019-07-04 22:45发布

此代码创建一个通知。 如果你点击它,目前的应用程序运行(目的是创建Entry ,这是我唯一的Activity ,一个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是创建),或捕捉任何输出或输入Intents ,当我点击我新做的Notification

PS。 我道歉,如果这是一个重复线程,因此是相当缓慢的,现在,我无法正常搜索。

Answer 1:

添加android:launchMode="singleTop"在你的activity在您的清单文件,已在法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);
}


Answer 2:

这个工作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 /构造做到这一点:

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

    }


Answer 3:

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


文章来源: Android call method on notification click