5

I am trying to send an intent from a non system app using the following function.

public static void sendIntent() {
        if (null != _context) {
            Intent intent = new Intent("com.test.testApp.testIntent");
            intent.setPackage(_context.getPackageName());
            _context.sendBroadcast(intent);
        }
    }

But I always see there is an error message from ActivityManager as below. The same intent broadcasting(app) works fine in andorid 6.0 but throws an error in android 7.1.1. I am required to change anything for android 7.1.1?

4-10 15:06:34.423 1615 2921 E ActivityManager: Sending non-protected broadcast com.test.testApp.testIntent from system 2886:com.test.testApp/u0a117 pkg com.test.testApp

In a ListFragment I register the receiver as follows:

 @Override
    public void onStart() {
        super.onStart();
        getActivity().registerReceiver((receiver),
                new IntentFilter(com.test.testApp.testIntent));
        TextView textDownload = (TextView) getActivity().findViewById(R.id.output);
        textDownload.setVisibility(android.view.View.INVISIBLE);
    }
savi
  • 497
  • 2
  • 12
  • 27
  • Check Background Optimizations in this link: https://developer.android.com/about/versions/nougat/android-7.0-changes.html Hope this helps – Sneh Pandya Apr 11 '17 at 19:39
  • What is the actual action string? I doubt that it is `"com.test.testApp.testIntent"`. – CommonsWare Apr 11 '17 at 19:51
  • @SnehPandya I am not using any of these CONNECTIVITY_ACTION , ACTION_NEW_PICTURE or ACTION_NEW_VIDEO. How does this apply to my issue? – savi Apr 11 '17 at 20:09
  • @CommonsWare Action string is "com.testApp.savi.STATUS_CHANGE" – savi Apr 11 '17 at 20:16
  • 3
    `:: shrug ::` Try changing the name, particularly the `STATUS_CHANGE` bit, in case there is some strange bug when Android examines action strings to see what can and cannot be sent. Also note that implicit broadcasts like this do not work on Android O, once your `targetSdkVersion` rises to `O` or higher (or whatever `O` turns into once Android O ships in final form). You may wish to consider using something else. – CommonsWare Apr 11 '17 at 20:26
  • @CommonsWare Thanks that worked. – savi Apr 11 '17 at 20:45
  • @CommonsWare My app was never a system app. Why do I get this error. Also what are the different ways that I can make my app a system app. – savi Apr 11 '17 at 21:12
  • 1
    "Why do I get this error" -- a bug in Android, perhaps. What device or emulator are you using for testing this? "Also what are the different ways that I can make my app a system app" -- build your own custom ROM. – CommonsWare Apr 11 '17 at 21:14
  • @CommonsWare I meant how can I make an android a system app? I know we can use "android.uid.system" in androidManifest.xml. Is there any other way to make an android app a system app? – savi Apr 11 '17 at 21:23
  • 3
    "Is there any other way to make an android app a system app?" -- no. And the only way you can use `android.uid.system` is if your app is signed by the same signing key that signs the rest of the pre-installed apps, and that requires you to build a custom ROM. – CommonsWare Apr 11 '17 at 21:24
  • 1
    FWIW, I cannot reproduce your problem. Sending a broadcast with `sendBroadcast(new android.content.Intent("com.testApp.savi.STATUS_CHANGE"));` works just fine on Android 7.1, when I put that code in `onCreate()` of an activity. I do not get the error that you cited. – CommonsWare Apr 11 '17 at 23:29
  • @CommonsWare I use this intent in a service rather than an activity. – savi Apr 12 '17 at 18:25
  • I do not get that crash when broadcasting it from an `IntentService` either. If you get a chance to create a reproducible test case, consider [filing an issue](http://b.android.com). – CommonsWare Apr 12 '17 at 18:28
  • Can you make your app a system app and check if you get the same issue? – savi Apr 12 '17 at 18:32

1 Answers1

2

This might help,

If you have in your AndroidManifest.xml declared "android:sharedUserId="android.uid.system", then declare the protected broadcast.

Reference : https://stackoverflow.com/a/50240471

DesignIsLife
  • 510
  • 5
  • 11