Broadcast not triggering - android

I have been teaching myself android programming and I've run up against a problem with broadcasts. Essentially this code is just me testing to see if I can get broadcasts to work. When I run it my broadcast is not called and I'm not sure why.
Here is the relevant bit of manifest
<reciever
android:name="application.logic.StartEventReciever"
android:label="#string/title_activity_start_event" >
<intent-filter>
<action android:name="Set Start Alarm" />
</intent-filter>
</reciever>
Here is my BroadcastReciever
public class StartEventReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.v("#StartEvent", "BOO");
}
}
And here is the bit where I make the intent. It's a part of a larger static class I've been using.
public static void setStartAlarm(Context context){
Intent intent = new Intent("Set Start Alarm");
context.sendBroadcast(intent);
}

Your <reciever tag is spelled wrong. It is <receiver> </receiver>

Related

Set InnerClass broadcastreceiver to android action

This is what I am using right now:
in the onCreate method:
registerReceiver(bootup, new IntentFilter("android.intent.action.BOOT_COMPLETED"));
the bootup receiver:
BroadcastReceiver bootup = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("test", "received");
}
};
These are both in the MainActivity class of my app. What I want is for the bootup receiver to be called when the bootup of the phone is complete. I need it in the MainActivity class because I need to access a few things from it.
How would I set this? My current solution does not work.
EDIT: The posted solution seems as though it will work for my purpose, and this is what has been suggested by other threads. However, when I put a log statement in the receiver that it ties to, nothing appears in the console. Is this because the application is not running? I have also tried sending a notification with notificationmanager
EDIT 2: I took advice and switched to a broadcastreceiver in a another class for detecting the reboot. It works by simply changing the name attribute on the manifest file. This being the reason why I cant use an inner class receiver doesn't really make any sense compared to what I have seen on other questions. Can someone explain why I can't point the receiver to the inner class one and why I have to use a separate class?
Create a Receiver:
public class BootupReceiver extends BroadcastReceiver {
private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
if (ACTION_BOOT.equals(intent.getAction()))
Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();
}
}
And receiver and uses-permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.restarttest.BootupReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

How to check if my BroadcastReceiver receives the BOOT?

I have a BroadcastReceiver who should receive the BOOT.
Is there a simple way to check if the BroadcastReceiver receives the BOOT correctly?
Thanks
Assuming BOOT means BOOT_COMPLETED then you could use the following:
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
//Do a Log or, less likely, a Toast or start your application here.
}
}
};
You would register this in the manifest like such:
<receiver android:name="com.example.yourAppName.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>

Broadcast receiver not triggered after app is stopped

I have a broadcast receiver defined in the manifest with the android.bluetooth.device.action.ACL_CONNECTED in the intent filter.
It's triggered fine when the app is in the stack, but after I stop it from android settings it won't trigger anymore. any suggestions?
Update for Menny:
<receiver android:name=".auto.AppLauncher">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
</intent-filter>
</receiver>
Did you program a BroadcastReceiver in your Code?
public class receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
//something you want to do here
}
}
}

How to start an activity via a service after rebooting

I need my app to start running (in the background) after rebooting the device. Here's what I've come up with till now (after taking a lot of help from here...)
This is my BootUpReceiver making use of broadcastreceiver:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, RebootService.class);
serviceIntent.putExtra("caller", "RebootReceiver");
context.startService(serviceIntent);
}
}
This is the service class:
public class RebootService extends IntentService{
public RebootService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
protected void onHandleIntent(Intent intent) {
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String intentType = intent.getExtras().getString("caller");
if(intentType == null)
return;
if(intentType.equals("RebootReceiver"))
getApplication().startActivity(i);
}
}
THis is my android manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service android:name=".RebootService"/>
</application>
The problem is, When I install this on my phone and reboot, the app crashes: It says, "Transfer has stopped working". After I press the OK button, when I check the app info, the app is running.
I'm new to android and I'm not sure what is going on. Am I supposed to add any more permissions?
Kindly help.
TIA
I think your problem lies with your RebootService constructor. When the system invokes it, it doesn't provide any arguments, so it's going to crash. If you look in the logs you'll probably see something to the effect of "Unable to instantiate service..."
Try replacing your constructor with:
public RebootService() {
super( "Reboot Service" );
}

I want to start system application activity in broadcast onReceive() methord

I want to start a system application activity in a broadcast onReceive() method, but it cannot be run. I need help!
My Manifest.xml
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
My java:
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent b_intent = new Intent();
b_intent.setComponent(new ComponentName("com.android.email", "com.android.email.activity.Welcome"));
b_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(b_intent);
}
}
But this email application can not be run. There is only black color on the screen.
Thanks!
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent()
i.setClassName("com.android.email", "com.android.email.activity.Welcome");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i); }
Launching system or third party apps from your app should be done through implicit intents. Hard-coding the package names and component names is not reliable and may not work all the time.
Morever, this particular activity, I guess, is not allowed to be called from other apps (my assumption, I might be wrong)

Categories

Resources