I have registered a static receiver inside my Manifest but I don't know why my onReceive is never called. I have searched alot and checked several solutions but none of them worked as they are all identical.
Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendBroadcast(new Intent("com.example.androidsinglebcreceiver.show_toast"));
}
});
}
@Override
protected void onResume() {
super.onResume();
}
}
BroadcastReceiver:
public class Receiver extends BroadcastReceiver {
private final String TAG = "Receiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "INTENT RECEIVED");
Toast.makeText(context, "INTENT RECEIVED by Receiver", Toast.LENGTH_LONG).show();
}
}
Manifest:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.example.androidsinglebcreceiver.show_toast" >
</action>
</intent-filter>
</receiver>
</application>
try to send your broadcast like this.
sendBroadcast(new Intent(MainActivity.this,Receiver.class).setAction("com.example.androidsinglebcreceiver.show_toast"));