AnD*_*Dev 30 notifications android
我有一个带有两个按钮的应用程序.一个"关闭"应用程序的按钮和一个开始算法的按钮.当我点击"开始"时,它"隐藏"应用程序并在通知栏中显示通知.我需要能够在单击/按下通知时执行/调用方法.这类问题有几个答案,但它们非常模糊,只能指向BroadcastReceiver上文档的链接.
如果您要将一个URL留给BroadcastReceiver文档并说"阅读本页",请不要回复此问题.如果您要解释如何使用BroadcastReceiver来执行一个方法(从显示通知的同一个类中),请向我展示一些如何完成此操作的代码.
我的算法:按下按钮,显示通知,点击通知,调用方法(不显示活动).而已.
如果不可能,请告诉我.如果是,请告诉我你将做些什么来使它成为可能.这个简单的东西不应该被android sdk的开发人员忽视.
Ton*_*ham 35
经过几次试验和错误的迭代后,我终于找到了一种在单击通知操作时运行任意方法的相当简单明了的方法.在我的解决方案中,有一个类(我将其称为NotificationUtils)创建通知,还包含一个IntentService静态内部类,该类将在单击通知上的操作时运行.这是我的NotificationUtils类,然后是对AndroidManifest.xml的必要更改:
public class NotificationUtils {
public static final int NOTIFICATION_ID = 1;
public static final String ACTION_1 = "action_1";
public static void displayNotification(Context context) {
Intent action1Intent = new Intent(context, NotificationActionService.class)
.setAction(ACTION_1);
PendingIntent action1PendingIntent = PendingIntent.getService(context, 0,
action1Intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Notification")
.setContentText("Notification text goes here")
.addAction(new NotificationCompat.Action(R.drawable.ic_launcher,
"Action 1", action1PendingIntent));
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
public static class NotificationActionService extends IntentService {
public NotificationActionService() {
super(NotificationActionService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
DebugUtils.log("Received notification action: " + action);
if (ACTION_1.equals(action)) {
// TODO: handle action 1.
// If you want to cancel the notification: NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在只需在onHandleIntent中实现您的操作,并将NotificationActionService添加到<application>标记内的清单中:
<service android:name=".NotificationUtils$NotificationActionService" />
Run Code Online (Sandbox Code Playgroud)
摘要:
Dau*_*fin 11
通过通知点击我们无法获取任何火灾事件或任何点击监听器,同时在通知栏中添加您的应用程序,我们用来设置一个通常启动活动的待处理意图,一旦点击或按下通知.但我有一个解决方案,如果你真的不想显示你的活动,那么以待定意图开始的活动会从那里发送一个广泛的演员到你的父活动,然后完成待处理的活动,然后一旦广播接收器在父活动中接收调用你想要的接收器内的任何方法.供你参考..
// This is what you are going to set a pending intent which will start once
// notification is clicked. Hopes you know how to add notification bar.
Intent notificationIntent = new Intent(this, dummy_activity.class);
notificationIntent.setAction("android.intent.action.MAIN");
notificationIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT |
Notification.FLAG_AUTO_CANCEL);
// Now, once this dummy activity starts send a broad cast to your parent activity and finish the pending activity
//(remember you need to register your broadcast action here to receive).
BroadcastReceiver call_method = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action_name = intent.getAction();
if (action_name.equals("call_method")) {
// call your method here and do what ever you want.
}
};
};
registerReceiver(call_method, new IntentFilter("call_method"));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36150 次 |
| 最近记录: |