【问题标题】:Use pending intent created in Service class in MainActivity (Android)使用在 MainActivity (Android) 的 Service 类中创建的待处理意图
【发布时间】:2015-08-23 10:18:38
【问题描述】:

所以我试图将我在我的服务类中创建的待处理意图放入我的主活动中,这样我就可以在单击按钮时使用它。如果有人想知道为什么我需要这个 Intent,它是因为 NotificationListener 获取通知意图,所以当我单击按钮时,我可以打开该意图以进入应用程序,通知最初来自。

通知服务类

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();
        }




    }

【问题讨论】:

  • 好的。对你有益。但通常人们会问问题。
  • @njzk2 我问了一个有效的问题我不知道为什么这个社区总是让我觉得自己很愚蠢,并让我回答一个合理的问题。
  • 你没有问问题,这就是问题
  • @njzk2 问题是:如何在 MainActivity 类的 NotificationService 类中使用待处理的意图?

标签: java android android-intent android-pendingintent


【解决方案1】:

据我了解,您想要的是您的 Service 与 Activity 进行通信的一种方式。

有很多方法可以做到,视情况而定。

Using a LocalBroadcast 是一种可能的方法。

您也可以查看LocalService example

还可以查看BoundServices

由于您没有提供更多上下文,因此很难推荐一个特定的,但最简单的可能是LocalBroadcast

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多