I want to auto start my service when device is rebooted.Please do not mark this as duplicate of Question, but solution given there are not working!
BootUpReceiver.java (BroadcastReceiver)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.util.Map;
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shubham.servic">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:enabled="true"
android:name=".BootUpReceiver"
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=".MyService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
String msg = "Android : ";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
But still service is not starting itself on reboot.
I have one more different question:
I have to run my background service in fixed interval of time, So far I have created an AlarmManager inside service itself which is calling same service after some interval, again and again ,How can I achieve "running background service at fixed interval of time in more efficient way?"
Because of restrictions in Android Oreo you can't start services while in the background. source with detailed info
Like the source suggests you should use scheduled jobs instead. The easiest way to manage this is to use a library like android-job or workmanager. Android Job is a much used 3rd party library developed by Evernote, but they've announced support will end when Workmanager, which is still in alpha, has been in production for a while.
Related
I have an application without any activity but just a receiver. I regiter the receiver and then install the apk using adb. However, I never reach this receiver. Why Do I never reach the receiver (for example while rebooting)?
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="amiin.bazouk.application.com.doproject">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".DeviceOwnerReceiver">
</receiver>
</application>
</manifest>
DeviceOwnerReceiver.java:
package amiin.bazouk.application.com.doproject;
import android.app.Activity;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class DeviceOwnerReceiver extends DeviceAdminReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.intent.action.BOOT_COMPLETED")){
System.out.println("ADRIEN");
}
if(action.equals("android.intent.action.BATTERY_CHANGED")){
System.out.println("ADRIEN");
}
}
#Override
public void onProfileProvisioningComplete(Context context, Intent intent) {
if (Util.isDeviceOwner(context)){
//start enforcing policies: example if kioskModeEnabled->startKiostMode
}
}
#Override
public void onLockTaskModeEntering(Context context,Intent intent,String pkg) {
if (Util.isDeviceOwner(context)){
//start enforcing kiosk mode policies like this example when this property is done:
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Activity.DEVICE_POLICY_SERVICE);;
dpm.setStatusBarDisabled(new ComponentName(context.getApplicationContext(), DeviceOwnerReceiver.class),false);
}
}
#Override
public void onLockTaskModeExiting(Context context,Intent intent) {
//exit kioskmode
}
}
I think you need to setup RECEIVE_BOOT_COMPLETED permission, and also configure intent filter to your service like BOOT_COMPLETED, QUICKBOOT_POWERON.
Look at this, they answer explain very well the issue
https://stackoverflow.com/a/46294732/7774671
There are few things missing in the receiver in the manifest. Try this.
<receiver
android:name=".DeviceOwnerReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
A broadcast receiver will not be invoked if it is not registered with an intent. This association receiver / intent can be declared in the manifest using intent filter.
<receiver android:name=".DeviceOwnerReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<action android:name="android.intent.action.BATTERY_CHANGED"></action>
</intent-filter>
</receiver>
I want to start a service after reboot. The problem I have is that this does not happen every time (at least the first 20 minnutes). I have study many questions into stackoverflow and try a number of the provided solutions however sometimes the service does not automaticaly start after reboot.
Also I have to add another parameter this of the foreground service for android versions O and above.
Could someone give me any advice?
AndroidManifest.xml
.... <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
>
<activity
android:name=".Activities.MainActivity"
....
</activity>
<activity
...
</activity>
<receiver android:name=".Helpers.BootCompletedIntentReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- I think that this is also not necessery <category android:name="android.intent.category.DEFAULT" /> -->
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="true"></service>
</application>
BroadcastReciever
package com.abc.Helpers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.abx.Activities.MainActivity;
import com.abc.Services.myService;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, myService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context,serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
I also found in a comment here that reciever should be registered into an activity of the application and this is the code in Mainactivity. Do you agree?
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootCompletedIntentReceiver.class.getName());
if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
..
}
I'm trying to write an battery monitoring application, and one of its function is to keep track of the number of time being recharged and the date associated with it.
First, I wrote everything in the main activity class using SharedPreferences, and it does work. However, I wish to keep track of the number of recharge even in background, so I googled and figured that WakefulBroadcastReceiver + IntentService might be the most efficient/easy way to do it.
I was wrong. I stuck here and I think the problem I'm having right now is that the monitor (the IntentService part) doesn't even start! I think I might have put something wrong in the manifest but I just can't find it.
Here're my codes:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mobiledevices.batteryapp" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".RechargeMonitor"
android:exported="false" />
</application>
BootReceiver.java
package com.mobiledevices.batteryapp;
import android.content.Intent;
import android.content.Context;
import android.support.v4.content.WakefulBroadcastReceiver;
public class BootReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Intent startServiceIntent = new Intent(context, RechargeMonitor.class);
startWakefulService(context, startServiceIntent);
}
}
RechargeMonitor.java
package com.mobiledevices.batteryapp;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.content.SharedPreferences;
import android.os.BatteryManager;
import android.widget.Toast;
public class RechargeMonitor extends IntentService {
public RechargeMonitor(){
super("RechargeMonitor");
}
#Override
protected void onHandleIntent(Intent intent){
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
this.registerReceiver(this.batteryinfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batteryinfoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT) &&
intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)!=0 &&
intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0)==BatteryManager.BATTERY_STATUS_CHARGING){
SharedPreferences settings = getSharedPreferences(MainActivity.PREF_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
int count = settings.getInt("COUNT", 0);
editor.putInt("COUNT", count+1);
editor.commit();
}
}
};
}
The MainActivity.java is too long so I don't post it here. However, it shouldn't have any problem with that unless I have to call either BootReceiver/RechargeMonitor in the activity file which I guess I don't have to since manifest + BootReciver should be taking care of autostart, and it won't even be a problem.
Again the problem I encounter now is RechargeMonitor doesn't seem to start (I don't see any Toast message.)
Another thing for me to make sure is that SharedPreferences should be accessible everywhere in the same application as long as I have the name right. I've seen that in several places but I need a solid answer.
Thanks!
I just want to launch activity via Secret Code .
Although , there are many solutions available for this problem but none of them worked for me. It seems like BroadcastReceiver is not listening .
I am using Mi4i MIUI 7.
Here's my code. Please Help!!
MainActivity.java
package com.example.jatin.myapplication2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have even tried the below commented code , but that didn't work.
MySecretCodeReceiver.java
package com.example.jatin.myapplication2;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Jatin on 05-Aug-16.
*/
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
String uri = intent.getDataString();
String sep[] = uri.split("://");
if (sep[1].equalsIgnoreCase("1234"))
{
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.example.jatin.myapplication2.MainActivity");
context.startActivity(launchIntent);
}
else if (sep[1].equalsIgnoreCase("5678")) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("net.one97.paytm");
context.startActivity(launchIntent);
}
// Intent i = new Intent(context, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// setResultData(null);
// context.startActivity(i);
}
}
}
I have also tried putting android:host="1234" in manifest ,still not getting desired results
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
<receiver android:name="receivers.MySecretCodeReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.PROCESS_OUTGOING_CALLS">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" />
</intent-filter>
</receiver>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Got it ....
We have to register our MySecretCodeReceiver as a <receiver> within the <application> element in the AndroidManifest.xml file.
And , Its Done. :)
I have a problem with running the service. In the logs there is nothing to see that the service is running. So I don't know that she works. Toast also does not show in MainActivity. I read a lot of posts and none work. How to fix it?
Service
import android.app.Service;
import android.os.IBinder;
import android.widget.Toast;
import android.content.Intent;
public class AutoStartUp extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
BroadcastReceiver
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Intent;
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AutoStartUp.class);
context.startService(serviceIntent);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kamiszczu.ovh.servicetest3">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name="kamiszczu.ovh.servicetest3.BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="kamiszczu.ovh.servicetest3.AutoStartUp"
android:enabled="true">
</service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You don't have to verify the intent type since the
Receiver is registered with only one intent type(Intent.ACTION_BOOT_COMPLETED). So there is no need to check in the receiver if the intent action is Intent.ACTION_BOOT_COMPLETED.
I think the condition in the receiver is not true and because of that the code that starts your service is not executed.