此代码创建一个通知。如果单击它,将运行当前应用程序(意图是在Entry
中创建的,这是我唯一的Activity
),这是Android Developers博客的稍作修改的版本:
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);
}
但是我不想启动任何 Activity ,而只想在当前 Activity 中运行一个方法。从到目前为止的内容来看,我猜想我必须使用
startActivityForResult()
之类的方法,使用intent-filters
并实现onActivityResult()
的方法,但是在弄乱了所有这些内容之后,更改了Intent
和PendingIntent
中的内容之后,我仍然没有可用的结果。是否有可能以某种方式仅在Entry
(我的主要Activity
,在其中创建Intent
的创建方法)中调用一个方法,或者在单击新创建的Intents
时捕获任何传出或传入的Notification
?PS。我很抱歉,如果这是一个重复的线程,那么现在很慢,我无法正确搜索。
最佳答案
在 list 文件的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);
}
关于android - 通知点击时的Android调用方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13822509/