[英]Android call method on notification click
This code creates a notification. 此代码创建通知。 If you click it, the current application is ran (the intent is created in Entry
, which is my only Activity
), a slightly modified version of a Android Developers blog: 如果单击它,则运行当前应用程序(意图在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);
}
But I don't want to start any activity, but merely to run a method in the current activity. 但我不想开始任何活动,而只是在当前活动中运行一个方法。 From what I've read so far, I guess I have to use methods like startActivityForResult()
, use intent-filters
and implement onActivityResult()
, but after messing around with all those things, changing things in the Intent
and PendingIntent
, I still have no usable result. 从我到目前为止所读到的,我想我必须使用像startActivityForResult()
这样的方法,使用intent-filters
并实现onActivityResult()
,但是在搞乱了所有这些事情后,改变了Intent
和PendingIntent
中的东西,我仍然没有可用的结果。 Is it possible to somehow just call a method in Entry
(my main Activity
, in which the Intent
is created), or catch any outgoing or incoming Intents
when I click my newly made Notification
? 是否有可能以某种方式调用Entry
的方法(我的主Activity
,其中创建了Intent
),或者在我点击新制作的Notification
时捕获任何传出或传入的Intents
?
PS. PS。 my apologies if this is a duplicate thread, SO is quite slow right now, I can't search properly. 我很抱歉,如果这是一个重复的线程,那么现在很慢,我无法正常搜索。
Add android:launchMode="singleTop"
in your activity
in your manifest file, have the method protected void onNewIntent(Intent intent) { ... }
and use this code: 在您的清单文件的activity
中添加android:launchMode="singleTop"
,让方法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);
}
This worked 100% for me: 这对我来说100%有效:
Place this code in a method: 将此代码放在一个方法中:
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);
}
Then in the onCreate/constructor of your class do this: 然后在你的类的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 :)
}
}
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.