通知点击的Android通话方法

ste*_*ong 17 android android-intent android-notifications android-activity

此代码创建通知.如果单击它,则运行当前应用程序(意图创建于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);
}
Run Code Online (Sandbox Code Playgroud)

但我不想开始任何活动,而只是在当前活动中运行一个方法.从我到目前为止所读到的内容来看,我想我必须使用像startActivityForResult()使用intent-filters和实现这样的方法onActivityResult(),但是在弄乱了所有这些东西之后,改变了Intent和中的东西PendingIntent,我仍然没有可用的结果.是否有可能以某种方式调用一个方法Entry(我的主要Activity,在其中Intent创建),或者Intents当我点击我的新制作时捕获任何传出或传入Notification

PS.我很抱歉,如果这是一个重复的线程,那么现在很慢,我无法正常搜索.

MC *_*ror 13

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);
}
Run Code Online (Sandbox Code Playgroud)


小智 5

这为我工作了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);
    }
Run Code Online (Sandbox Code Playgroud)

然后在类的onCreate / constructor中执行以下操作:

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

    }
Run Code Online (Sandbox Code Playgroud)