Syed
Syed

Reputation: 550

Why I am not receiving the installed apps broadcast?

I am trying to get all installed applications using intents and broadcast receiver, but the problem is I never directs to my onReceive method and so not getting any package name. I am using the following code:

KillAppBCR.java

public class KillAppBCR extends Activity {
private static final String TAG = "BroadcastReceiver";

BroadcastReceiver receiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);

    receiver = new TestBroadcastReceiver();

    registerReceiver(receiver, filter);

    Log.e(TAG, "onCreate");
    Toast.makeText(KillAppBCR.this,"onCreate",Toast.LENGTH_SHORT).show();

}

TestBroadcastReceiver.java

public class TestBroadcastReceiver extends BroadcastReceiver
 {
 private static final String TAG = "TestBroadcastReceiver";

@Override
public void onReceive(Context context, Intent intent)
{
    String actionStr = intent.getAction();

    Log.e(TAG, "onReceive");
     Toast.makeText(context,"onReceive",Toast.LENGTH_SHORT).show();


    if (Intent.ACTION_PACKAGE_ADDED.equals(actionStr)) {

        Uri data = intent.getData();

    }
}
} 

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.KillAppBCR"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".KillAppBCR"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<receiver android:name=".TestBroadcastReceiver">
  <intent-filter>
    <action android:name="com.KillAppBCR" />
  </intent-filter>
</receiver>
</application>
</manifest>

Log Cat

07-19 18:39:48.768: ERROR/BroadcastReceiver(512): CC

07-19 18:39:49.008: INFO/ActivityManager(58): Displayed activity    com.KillAppBCR/.KillAppBCR: 685 ms (total 685 ms)

07-19 18:39:54.338: DEBUG/dalvikvm(121): GC_EXPLICIT freed 259 objects / 12032 bytes in 157ms

07-19 18:42:58.943: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol

07-19 18:47:58.989: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol

Where is the problem ? Please help me out, I want to receive broadcasts of all installed apps.

Thanks

Upvotes: 2

Views: 2019

Answers (2)

Ahmed Belal Hashmi
Ahmed Belal Hashmi

Reputation: 1

I think you have to make sure that your application runs beyond just a gui, i.e. perhaps it should have a service component as well which could note all installations being made. IMHO you are trying to read only new installations, right? Because that is what the code should do, however as TomTasche pointed out, clearly your application will get killed if you navigate away from it. And once your application is dead, nothing, nada. No intents, no notifications, simple.

Upvotes: 0

TomTasche
TomTasche

Reputation: 5526

Why your Activity code most probably doesn't work: You're registering the receiver only temporarily with registerReceiver. So Android will kill your receiver again after you closed your Activity.

What you have to do is replace com.KillAppBCR with android.intent.action.PACKAGE_ADDED in your AndroidManifest.xml. This tells Android to register your receiver permanently.

Upvotes: 3

Related Questions