1

I am trying to execute another app from my app using PendingIntent. I get other apps' pending Intent value and do this (I got pending intent value in String type)

public class NotificationCrawlingService extends NotificationListenerService {

AppDatabase appDatabase;
WordDatabase wordDatabase;
NotificationDatabase notificationDatabase;
Context context;
PackageManager pm;
NotificationEntity ne;
AllFragment allFragment;
RecyclerViewAdapter rv;
List<NotiData> notiData;
NotificationEntity noti;
Notification notification;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
    context = getApplicationContext();
    appDatabase = AppDatabase.getAppDatabase(context);
    wordDatabase = WordDatabase.getWordDatabase(context);
    notificationDatabase = NotificationDatabase.getNotificationDatabase(context);
    pm = context.getPackageManager();
    notification = sbn.getNotification();
    Bundle extras = notification.extras;
    String pakage_name = sbn.getPackageName();
    String app_name = findApp_name(pakage_name);
    PendingIntent pendingIntent = notification.contentIntent;


Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(pendingIntent));
        startActivity(intent1);
        

However it won't work. Shows this error in Log

2020-08-17 16:29:27.197 4503-4503/com.example.alimseolap1 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.alimseolap1, PID: 4503 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=PendingIntent{78fb01: android.os.BinderProxy@b9c2b85} } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2018) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673) at android.app.Activity.startActivityForResult(Activity.java:4689) at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676) at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234) at androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:791) at androidx.fragment.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:933) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1185) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1173) at com.example.alimseolap1.views.Fragment.AllFragment$2.onReceive(AllFragment.java:170) at androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313) at androidx.localbroadcastmanager.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7050) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

What might be the problem?

4

1 回答 1

0

You can't convert a PendingIntent into a Uri like that. Instead of:

PendingIntent pendingIntent = notification.contentIntent;
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(pendingIntent));
startActivity(intent1);

You do this:

PendingIntent pendingIntent = notification.contentIntent;
pendingIntent.send();

This launches the Intent that was wrapped in the PendingIntent.

于 2020-08-18T09:34:16.867 回答