beginner
beginner

Reputation: 35

Android onReceive in BroadcastReceiver not working

I added a receiver to listen when app is installed. But it is not working. Here is my code in AndroidManifest.xml

 <receiver android:enabled="true" 
   android:exported="true"
   android:name="com.bsp.iqtest.reiceiver.IQTestReceiver">
   <intent-filter>

     <action android:name="android.intent.action.PACKAGE_ADDED" />
     <action android:name="android.intent.action.PACKAGE_REPLACED" />
     <data android:scheme="package"/>
   </intent-filter>
</receiver>

Here is my code in MainActivity (launcher activity) , function onCreate.

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   IQTestReceiver br = new IQTestReceiver();
   IntentFilter intentFilter = new IntentFilter();
   intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
   intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
   intentFilter.addDataScheme("package");
   registerReceiver(br, intentFilter);
}

Here is my code in IQTestReceiver (this class is written in other file)

public class IQTestReceiver extends BroadcastReceiver {
  public IQTestReceiver() {
  }
  @Override
  public void onReceive(Context context, Intent intent) {
    String packageName=intent.getData().getEncodedSchemeSpecificPart();
    Log.e("HELLO",packageName);
  }
}

I set a breakpoint in onReceive function , but it doesn't run when i debug.

Thanks for your helping.

Upvotes: 1

Views: 673

Answers (2)

Lovis
Lovis

Reputation: 10047

You can not receive PACKAGE_ADDED or PACKAGE_REPLACED for your own app, if that is what you're trying.

"Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast."

See http://developer.android.com/reference/android/content/Intent.html

Upvotes: 1

Pratik
Pratik

Reputation: 456

set your broadcasrt in manifest like this

 <receiver
            android:name=".IQTestReceiver"
            android:exported="true"
            android:enabled="true">

            <intent-filter>
                <action android:name="check_values"/>

            </intent-filter>

        </receiver>

and send the broadcast like this.....Intent it1=new Intent(Intent.ACTION_USER_PRESENT); it1.setAction("check_values"); it1.putExtra("data_key1",message); sendBroadcast(it1);

and in on receive would be like this....

  @Override
        public void onReceive(Context context, Intent intent)
        {
           data1=intent.getStringExtra("data_key1");
           System.out.println("ffffff11" + data1);
      }

Upvotes: 0

Related Questions