Reputation: 3181
I have broadcast which is recieved on system boot as well as by an intent. This broadcast is to set alarm.
Intent intent = new Intent();
intent.setAction("recievers.BroadCastBootRec");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
getActivity().sendBroadcast(intent);
My Broadcast class :
@Override
public void onReceive(Context context, Intent intent) {
c = context;
Log.d("HirakDebug", "BroadCast Recieved");
getDatesFromDatabase();
getDateDifference();
setAlarmI();
}
Manifest.xml
<receiver android:name=".recievers.BroadCastBootRec"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
IT IS RECIEVED ON SYSTEM REBOOT BUT NOT BY THE INTENT.
Upvotes: 1
Views: 379
Reputation: 24848
Please add custom action on intent filter :
<action android:name="recievers.BroadCastBootRec" />
Example :
<receiver android:name=".recievers.BroadCastBootRec"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="recievers.BroadCastBootRec" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Upvotes: 1
Reputation: 8946
You have to register two actions and to make things clear. android:name is the Class name of the receiver you had written, not the action. So, for the below receiver your receiver class name will be "BroadCastBootRec"
<receiver android:name=".recievers.BroadCastBootRec"
android:label="BootReceiver">
<intent-filter>
<action android:name="com.example.BroadCastBootRec" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Intent intent = new Intent();
intent.setAction("com.example.BroadCastBootRec");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
getActivity().sendBroadcast(intent);
The android system will inform your receiver when ever an action BOOT_COMPLETED and com.example.BroadCastBootRec is triggered
public class BroadCastBootRec extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
c = context;
Log.d("HirakDebug", "BroadCast Recieved");
getDatesFromDatabase();
getDateDifference();
setAlarmI();
}
}
Upvotes: 1
Reputation: 7560
The code should be something like
MainActivity.java
public void broadcastCustomIntent(View view)
{
Intent intent = new Intent("MyCustomIntent");
EditText et = (EditText)findViewById(R.id.extraIntent);
// add data to the Intent
intent.putExtra("message", (CharSequence)et.getText().toString());
intent.setAction("com.javacodegeeks.android.A_CUSTOM_INTENT");
sendBroadcast(intent);
}
In MyBroadcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Extract data included in the Intent
CharSequence intentData = intent.getCharSequenceExtra("message");
Toast.makeText(context, "Javacodegeeks received the Intent's message: "+intentData, Toast.LENGTH_LONG).show();
}
}
In app's Manifest file
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="com.javacodegeeks.android.A_CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
Upvotes: 0