20

此代码创建一个通知。如果单击它,则会运行当前应用程序(intent 是在 中创建的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(), useintent-filters和 implement之类的方法onActivityResult(),但是在搞砸了所有这些事情之后,改变了 and 中的东西IntentPendingIntent我仍然没有可用的结果。是否有可能以某种方式调用Entry(我的 main中的方法Activity,在其中创建),或者当我单击新创建的时Intent捕获任何传出或传入?IntentsNotification

PS。如果这是一个重复的线程,我很抱歉,所以现在很慢,我无法正确搜索。

4

3 回答 3

15

在清单文件中添加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);
}
于 2013-01-26T18:01:57.697 回答
7

这对我来说 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/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 :)
        }

    }
于 2016-03-03T09:46:14.430 回答
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);
于 2015-04-29T08:27:28.963 回答