I add my background service run perfectly but my phone switch off and switch on then my reciver class in get action android.intent.action.BOOT_COMPLETED.but service not start.my code below please help me!!!
public class ReceiverCall extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
static final String ACTION1 = "android.intent.action.QUICKBOOT_POWERON";
@Override
public void onReceive(Context context, Intent intent) {
Log.v("log_tag", "Action :: "+intent.getAction());
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), + (1000 * 60 * 2), pendingIntent);
Log.v("log_tag", "REPEAT");
}
}
And i also add permission in manifeast.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.employeemanager.app.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:theme="@style/AppTheme" >
<activity
android:name="***8"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="***.MyAlarmService"
android:enabled="true" >
</service>
<receiver android:name="***.ReceiverCall"
android:enabled="true" >
<intent-filter>
<action android:name="***" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.LAUNCHER" >
</category>
</intent-filter>
</receiver>
</application>
To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.
The “wakeup” happens in form of a onReceive() callback method. The class BroadcastReceiver defines the method onReceive() . Only during this method your broadcast receiver object will be valid, afterwards the Android system will consider your object as no longer active.
Here are the top five reasons why a service may fail to launch when your server boots: Your Windows Service isn’t set to Start Automatically A window service can be configured NOT to start when your computer reboots. Indeed, a service can be set to startup only on demand, or entirely disabled so it cannot run at all.
Method To Start Android Service When Device Boot Completed. 1 Start the android service directly use intent. This is not a good choice because the background service is always run,... 2 Start a repeat alarm, and make the alarm call the background service when executing every interval time. This is a good... More ...
A window service can be configured NOT to start when your computer reboots. Indeed, a service can be set to startup only on demand, or entirely disabled so it cannot run at all. Start the Services Control Panel application. Find your service in the list and double-click it to show its properties.
Ensure that the Startup type field is set to Automatic. Note that Automatic (Delayed Start), where your service starts 1-2 minutes after all Automatic services have been launched, may also be acceptable. Some windows services depend on other services to support their work. Windows enforces these dependencies when booting.
Maybe you misstyped but if you want to make that Intent
work you should call :
context.startService(alarmIntent);
And let me know if this log Log.v("log_tag", "Action :: "+intent.getAction());
is shown on your Logcat please this one Log.v("log_tag", "REPEAT");
too.
I would suggest that your BroadcastReceiver
will be like this :
public class ReceiverCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("log_tag", "Action :: "+intent.getAction());
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
//make an intent to your Service as follows
Intent serviceIntent = new Intent(context, MyAlarmService.class);
context.startService(serviceIntent);
}
}
Then on your Service
in your onCreate
of your Service
add this :
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, Whatever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), + (1000 * 60 * 2), pendingIntent);
Log.v("log_tag", "REPEAT");
I would suggest also that you create a new BroadcastReceiver
and on it do your stuff that you want to repeat, and replace the whatever.class
by your BroadccastReceiver
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With