[英]Use pending intent created in Service class in MainActivity (Android)
So I'm trying to get a pending intent I created in my Service class into my Main Activity so I can use it when a button is clicked. 因此,我试图将在我的服务类中创建的待定意向添加到我的主活动中,以便在单击按钮时可以使用它。 If anyone is wondering why I need this Intent its because the NotificationListener grabs the notifications intent so when i click my button I can open that intent to get into the app the notification was originally from.
如果有人想知道为什么我需要这个Intent,因为NotificationListener会获取通知的Intent,所以当我单击按钮时,我可以打开该Intent进入通知最初来自的应用程序。
NotificationService Class NotificationService类
package com.apps.skytek.notify;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.WindowManager;
public class NotificationService extends NotificationListenerService {
private WindowManager windowManager;
Context context;
private AchievementUnlocked Notify;
PendingIntent notifIntent;
NotificationManager mNotificationManager;
private StatusBarNotification sbn;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
mNotificationManager = (NotificationManager) getSystemService("notification");
}
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = sbn.getNotification().tickerText.toString();
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
Log.i("Package", pack);
Log.i("Ticker", ticker);
Log.i("Title", title);
Log.i("Text", text);
Intent msgrcv = new Intent("Msg");
msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
Notification notification = sbn.getNotification();
String s = sbn.getPackageName();
//cancelNotification(sbn.getKey());
notifIntent = notification.contentIntent;
try {
notifIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
From what I understood, what you want is a way for your Service to communicate with an Activity. 据我了解,您想要的是服务与活动进行通信的一种方式。
There are many ways to do it, depending on the case. 有多种方法,具体取决于情况。
Using a LocalBroadcast would be a possible approach. 使用LocalBroadcast将是一种可能的方法。
You can also check out LocalService example . 您还可以查看LocalService示例 。
Also check out BoundServices . 还请检查BoundServices 。
Since you haven't provided more context it's hard to recommend a specific one, but the simplest of all is probably the LocalBroadcast
. 由于您没有提供更多的上下文信息,因此很难推荐一个特定的上下文,但是最简单的可能是
LocalBroadcast
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.