简体   繁体   中英

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.

NotificationService Class

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.

You can also check out LocalService example .

Also check out 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM