I want my service to be started when android is rebooted. I used BroadcastReceiver to do so.
public class autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MyService1 mYourService = new MyService1();
MainActivity.instance_main.showToast("BroadCast Received...................");
Intent mServiceIntent = new Intent(MainActivity.instance_main, mYourService.getClass());
if (!MainActivity.instance_main.isMyServiceRunning(mYourService.getClass())) {
MainActivity.instance_main.startService(mServiceIntent);
MainActivity.instance_main.showToast("Restarted...................");
}
else {
MainActivity.instance_main.showToast("already running..................");
}
}
}
Receiver in Android Manifest
<receiver android:name=".autostart" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
Permissions defined
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Battery optimization ignore permission also requested in OnCreate()
Intent intent=new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+MainActivity.instance_main.getPackageName()));
MainActivity.instance_main.startActivity(intent);
But it won't work. Service won't start on reboot. Kindly Suggest me solution.
WE need to register the receiver. Add the following code to OnCreate()
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mService=new Autostart();
registerReceiver(mService,filter);
However after reboot it wont automatically hit.
Related
I was learning concept of broadcast receivers and I wanted to make a project with which I can demonstrate triggering specific implicit receiver class which is kind of explicit broadcast.
I made 2 apps the sender app and receiver app for demonstration :
BROADCAST SENDER's MAIN ACTIVITY :
public class MainActivity extends AppCompatActivity {
//Declaring our views
TextView senderTextView;
Button sendButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initializing views
senderTextView = findViewById(R.id.senderTextView);
sendButton = findViewById(R.id.sendButton);
//Setting onClick Listener on sendButton
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Calling Broadcast Method
Broadcast();
}
});
}
//Broadcast Method
private void Broadcast(){
//Creating private broadcast intent
Intent intent = new Intent("com.example.broadcastreceiver.PRIVATE_BROADCAST");
//Here we are going to find all the apps in our mobile that have registered for this broadcast in their manifests
//This will help us to find packages or apps registered for com.example.PRIVATE_BROADCAST action
PackageManager packageManager = getPackageManager();
//queryBroadcastReceivers of package manager will query all the receivers having intent filter for "com.example.PRIVATE_BROADCAST" action
//and store receivers info in resolveInfoList
List<ResolveInfo> resolveInfoList = packageManager.queryBroadcastReceivers(intent,0);
//Now we will iterate over this list to find our specific receiver and trigger it
//for each info in resolveInfoList
for (ResolveInfo info : resolveInfoList){
//if info's receiver class name is com.example.broadcastreceiver.CustomBroadcastReceiver (which is our receiver class in receiver app)
if(info.activityInfo.name.equals("com.example.broadcastreceiver.CustomBroadcastReceiver")){
//then use this info to get package name and receiver class name to make a componentName
ComponentName componentName = new ComponentName(info.activityInfo.packageName,info.activityInfo.name);
//now set this componentName to our intent
intent.setComponent(componentName);
}
}
//Sending our private broadcast to our android mobile
sendBroadcast(intent);
//setting textView
senderTextView.setText("Broadcast Sent!");
}
BROADCAST RECEIVER'S MANIFEST :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.broadcastreceiver">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.BroadcastReceiver"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".CustomBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcastreceiver.PRIVATE_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
BROADCAST RECEIVER's CustomBroadcastReceiver Class :
public class CustomBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Making a toast when broadcast is received
Toast.makeText(context, "Broadcast Receiver App : Custom Broadcast Receiver Triggered" , Toast.LENGTH_SHORT).show();
}
BroadcastReceiver's Mainactivity has nothing.
This should trigger the toast in CustomBroadcastReciever class of Broadcast Receiver App but due to some reason toast is not appearing please help.
Thanks in advance :)
Just created a demo app that works:
In app1, send the broadcast like this (obviously change package names):
public void sendBroadcast() {
Intent broadcastIntent = new Intent("com.example.testimplicitbroadcastreceiver.PRIVATE_BROADCAST");
broadcastIntent.setComponent(new ComponentName("com.example.testimplicitbroadcastreceiver",
"com.example.testimplicitbroadcastreceiver.CustomBroadcastReceiver"));
sendBroadcast(broadcastIntent);
}
In app2's manifest, register the BroadcastReceiver:
<receiver
android:name=".CustomBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.example.testimplicitbroadcastreceiver.PRIVATE_BROADCAST"/>
</intent-filter>
</receiver>
Voila, now app2 will receive your Broadcast in the CustomBroadcastReceiver.
I've some problem with Oreo and Broadcast Receiver on Boot. My method work for earlier version of Android, but with Oreo (Huawei P20 Lite with Pie) it doesn't work.
The Log.e in the Boot Receiver is not displayed in the LogCat and the action in BootService onCreate is not executed (No Log in log cat).
Boot Receiver
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Boot Receiver : Boot Start Service : AlarmNotification");
if ( Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) ) {
Intent myIntent = new Intent(context, BootService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(myIntent);
} else {
context.startService(myIntent);
}
}
}
}
I've set the permissions in the Manifest file
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service
android:name=".BootService"
android:enabled="true"
android:exported="true">
</service>
BootService
The Boot Service works on earlier versions of Android
public class BootService extends Service {
public BootService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
//Is fired by the Boot Receiver... on Boot !
Context context = this;
Log.e(TAG, "Start Alarm");
//Do something with the code
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
}
Perhaps I forget something, or do I something wrong ? Can somebody help me on this case, I already tested multiple solutions from StackeOverflow and read the doc, but for me it's not very clear for this case.
Thanks
I'm trying to create a service which starts when I plug the battery charger and stop when unplug it. The problem is when I unplug the charger my service don't stop while when I plug it's all fine.
Here is my code:
public class PowerConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){
Intent i = new Intent(context,MyService.class);
context.startService(i);
}
if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)){
Intent j = new Intent(context,MyService.class);
context.stopService(j);
}
}
}
Here is my Manifest:
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
I declared in AndroidManifest permission and receiver
<uses-permission android:name="android.permission.REAL_GET_TASKS"/>
<receiver android:name=".UninstallIntentReceiver">
<intent-filter android:priority="0">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
</receiver>
And here is my java class receiver
public class UninstallIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
Log.d("User selected: ",packageName);
}
}
}
When I open settings of other apps it works well - I get notification
(I can see in logs package name of chosen application), but
when I open settings my own application, I don't receive anything
I'm trying to set up an alarm receiver right after booting. Therefore, i have an OnBootReceiver that should register the alarm. The onBootReceiver works and it gets called, but somehow it cannot find my AlarmReceiver class.
OnBootReceiver which succesfully starts after booting:
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = "OnBootReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "called");
Intent i = new Intent(context, com.packagenames.AlarmReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, 30);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time.getTimeInMillis(), pi);
}
}
As you you can see, it configures the alarm tries to invoke com.packagenames.AlarmReceiver.class. This class exists and is located in the same package:
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "alarm received");
Intent i = new Intent(context, com.packagename.DataService.class);
i.putExtra("action", "process");
context.startService(i);
}
}
Unfortunately, i get the following error:
02-03 09:22:25.344: W/ActivityManager(103): Unable to start service
Intent { flg=0x4 cmp=com.phonegap.packagename/.AlarmReceiver (has extras)
}: not found
The Android Manifest looks like this
<application>
// activities etc
<receiver
android:name="com.phonegap.packagename.OnBootReceiver"
android:enabled="true"
android:exported="false"
android:label="OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="com.phonegap.packagename.AlarmReceiver"
android:enabled="true"
android:label="AlarmReceiver">
<intent-filter>
</intent-filter>
</receiver>
</application>
Do you see a mistake? Maybe I forgot something?
Thanks
edit: in the manifest, I added
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
in order to make the OnBootReceiver work. Do I need something similar for the alarm?
Shouldn't you use getBroadcast instead of getService when creating a pending intent ?
The whole receiver stuff ONLY works if you application is NOT installed on a SD card. Add this to your manifest file to do so:
android:installLocation="internalOnly"