I have a service that I would like to start on BOOT COMPLETE
when it is started , I have a toast message displayed.
my problem is that when the device boots up , the toast is displayed and is stuck on screen, and the service is not starting correctly.
however if I am trying to start my service through an activity , the service is starting well and the toast disappears after a few seconds correctly.
my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tfl.extprotocolservice"
android:versionCode="7"
android:versionName="1.6" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<application
android:allowBackup="true"
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.tfl.extprotocolservice.ExtProtocolBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".ExtProtocolService" >
<intent-filter>
<action android:name="com.tfl.extprotocolservice.ISetIpPort" />
</intent-filter>
<intent-filter>
<action android:name="com.tfl.extprotocolservice.IExtMessage" />
</intent-filter>
</service>
<!--
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
-->
</application>
</manifest>
my broadcast receiver:
public class ExtProtocolBroadcastReceiver extends BroadcastReceiver {
/* broadcast receiver to start on BOOT COMPLETE*/
#Override
public void onReceive(Context context, Intent intent) {
Intent StartServiceIntent=new Intent(context,ExtProtocolService.class);
context.startService(StartServiceIntent);
}
}
btw, the activity in the manifest is commented because I don't really need it , it was just to test starting the service from an activity.
If your application has no activities, your BroadcastReceiver will never get called.
When you install an application, it is installed in the "stopped state". applications in "stopped state" do not get broadcast Intents delivered to them.
In order to get your application out of "stopped state", the user must manually launch your application (at least once). In order to do this, you must offer him an Activity that he can use to start your application.
Once your application is no longer in "stopped state", Android will deliver broadcast Intents to it. That is, until the user "force stops" your application.
If the user "force stops" your application, it will go back to "stopped state" and will no longer get the broadcast Intents. Until the user manually starts your application again.
I tried with am broadcast -a android.intent.action.BOOT_COMPLETED then it restart the device.
You can try <action android:name="android.intent.action.USER_PRESENT"/>
After more research, I think it was the fastboot mode which will not broadcast BOOT_COMPLETE.
Your service is filtering actions, but your intent doesn't provide any.
Fix with this:
StartServiceIntent.setAction("com.tfl.extprotocolservice.IExtMessage");
Related
I know this has been asked tons of times, and there are hundreds of example on internet, but i want to understand what's wrong in my code.
As the title suggest i want to execute some code while the phone turn on, specifically i want to set some Alarms for getting notifications, but that's not relevant now, my problem is that the boot receiver onReceive method is never called apparently
I have the right permissions in the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
And i've also registered the receiver in the manifest
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
And I've already created the receiver class
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context pContext, Intent intent) {
Toast.makeText(pContext,"waiting for debugger",Toast.LENGTH_LONG).show();
android.os.Debug.waitForDebugger();
//Stuff for the alarms
}
}
Can someone explain me what i'm a failing without posting always the same examples that i see everywhere?
I want to know what's wrong in my code, not how it should be done.
PS: : I forgot to say that i need to stop the code for debugging the alarm things, but i don't think that's the problem since it doesen't even show the Toast.
UPDATE: full manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="feddycapdev.conapo.turnario" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<activity
android:name=".Calendario"
android:label="#string/title_activity_calendario"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Settings_Activity"
android:label="#string/title_activity_settings_" >
</activity>
<activity
android:name=".SettingGiorno"
android:label="#string/title_activity_setting_giorno" >
</activity>
<receiver
android:name=".BootBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:enabled="true" android:name=".WebNotificChecker" />
<service android:enabled="true" android:name=".Sveglia" />
<service android:enabled="true" android:name=".NotificaVigilanza" />
</application>
</manifest>
I should run the activity before the receiver?
Something has to use an explicit Intent to start one of your application's components before any manifest-registered receivers will work. For 99% of Android apps, that means that the user has to start your app from the home screen. If your app is serving as a plugin to some other app, you may not need the activity — please discuss this with the developers of the app that would be hosting your plugin.
how can I set notification if the user doesn't open activity?
You wouldn't set the notification if the user does not open the activity. Your app will only run when the user lets you run. If the user chooses not to start your app, or if the user chooses to "Force Stop" your app from within Settings, your manifest-registered receiver will not receive broadcasts.
I have registered a BroadcastReceiver called CheckReceiver:-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.captchachecker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:icon="#drawable/ic_launcher"
android:installLocation="internalOnly"
android:label="#string/app_name"
>
<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=".CheckReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
CheckReceiver code:-
public class CheckReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Check Receiver", "Captcha Receiver called");
Toast.makeText(context, "Receiver called", Toast.LENGTH_SHORT).show();
Intent service = new Intent(context, CheckService.class);
startWakefulService(context, service);
}
}
The problem is that when I press the power button and choose Power off option, BroadcastReceiver is not being called.
If I restart the device using:-
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
then BroadcastReceiver is being called.
I have read about launching activity atleast once for BroadcastReceiver and launch an activity 2-3 times before checking.
Turns out I had used:-
adb shell pm set-install-location 2
in the past on my device. The app got installed to sd card ignoring:-
android:installLocation="internalOnly"
After I moved the app to phone it started working properly. To move the app to phone go to:-
Settings->Apps->Your app and choose "Move to Phone"
So, if your boot receiver doesn't works. Check:-
Permissions and intent filter in the manifest are declared properly.
App has atleast one activity and it has been launched atleast once by user after installing or after the app is force closed.
App has been installed to internal storage.
On boot completed i am starting a service which runs perfectly on emulator but when i run it on android phone Broadcast receiver doesn't start service. Infact app is not even receiving boot completed broadcast from device.
This is my manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="com.darkrai.smsbasedcontroller.BootReciever"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</application>
This is my broadcast reciever class.
public class BootReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, UpdateService.class));
Log.d("Boot", "Boot Reciever");
}
I was having the same problem and its not your code problem or something.
I was facing this problem because of miui. Miui have the autostart permission disabled for every app.
If you are also using miui device you can do the following option
When you will allow autostart permisson your broadcast receiver will receive the ACTION_BOOT_COMPLETED.
You can allow autostart as
Setting>installed apps>your app> autostart.
Your BroadcastReceiver -- for any action -- will not receive any broadcasts until something on the device uses an explicit Intent to start one of your components. Usually, this comes in the form of the user tapping on an Activity of yours in the launcher.
So, add a LAUNCHER Activity, tap on it, and then you will receive broadcasts until:
the user force-stops you (typically by means of the Settings app), or
the user uninstalls your app
Some devices (mostly HTC) has a feature called fast reboot which doesn't trigger BOOT_COMPLETED. Instead they trigger QUICKBOOT_POWERON.
So add this permission <action android:name="android.intent.action.QUICKBOOT_POWERON" /> also.
i.e.
<receiver
android:name="com.darkrai.smsbasedcontroller.BootReciever"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
I have a broadcast receiver for incoming call.I want to launch a new activity when an incoming call comes.I am aware of the changes that are made from android 3.0,that the broadcast receiver will not work unless user manually starts an application
For that purpose I launch a dummy activity with just a toast message in it.Still the broadcast receiver is not working.
Here is my code
My broadcastreceiver
public class IncomingCallResult extends BroadcastReceiver
{
String TAG="IncomingCallResult";
#Override
public void onReceive(Context arg0, Intent I1)
{
Log.i(TAG,"inside on receive........");
Bundle bundle=I1.getExtras();
String state=bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent flash_intent=new Intent(arg0,LedFlasher.class);
flash_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(flash_intent);
}
}
}
manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blinker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".IncomingCallResult"
android:enabled="true">
<intent-filter
android:priority="214783648"
android:name="android.intent.action.PHONE_STATE">
</intent-filter>
</receiver>
<activity
android:name=".LedFlasher"
android:label="#string/title_activity_incoming_call_result" >
</activity>
<activity
android:name=".Dummy">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
What is wrong with the code?
Please help
As you have already known that the broadcast receiver will not work unless user manually starts an application For that purpose, you should not be surprised that your broadcast receiver will not work until user Manually open your application once. That is to say, you have to make a launcher activity which user is able to click and open it manually.
And what's more, it is better to Open and stay in your application for like 20s since I remember that the change of application configuration will take 10 or 20s to be saved.
I need to write data in to a file ,when system reboots not on boot complete.
i am using broadcast receiver "android.intent.action.REBOOT" Below is my code and manifest files
public class broadcastreceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.i("LOG_TAG","rebooted");
}
manifest file:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".broadcast"
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="com.example.broadcastreceiver.broadcastreceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.REBOOT">
<intent-filter>
<action android:name="android.intent.action.REBOOT" />
</intent-filter>
</receiver>
</application>
but i am not able to write even a log when reboots.
note:i donot want to use Bootcompleted action in broadcast receiver
I can't see why you don't want to use BootCompleted, could you provide your reasons?
There is no other action that will alert your broadcast receiver of the boot. You will have to use BootCompleted.
As a note, I hope you are registering you BroadcastReceiver with the context (since you didn't include that code). If you're not using BootComplete, I don't know what action you've registered to expect your above code to execute.
Add this, to me work, but on new devices, there are no testing.
For example in the recent nexus not working.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>