Receiver not reached - android

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>

Related

Android service does not start all the times after reboot

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);
..
}

BOOT_COMPLETED isn't received by my physical phone Asus

I tested a BroadcastReceiver which runs a JobIntentService in emulator, that worked successfully .
but it fails when i test the code to my physical phone ASUS ZE551ML .
Manifest file :
<?xml version="1.0" encoding="utf-8"?>
<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: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:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name=".MyJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="false"/>
</application>
BroadcastReceiver class :
package com.packt.backgroundservicedemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Write Code..
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, MyJobIntentService.class);
i.putExtra("sleepTime", 12);
MyJobIntentService.enqueueWork(context,i);
// context.startService(i);
}
}
}
JobIntentService class :
package com.packt.backgroundservicedemo;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.util.Log;
import android.widget.Toast;
public class MyJobIntentService extends JobIntentService {
static final int JOB_ID = 15;
static void enqueueWork(Context context,Intent intent){
enqueueWork(context,MyJobIntentService.class,JOB_ID,intent);
}
private static final String TAG = MyJobIntentService.class.getSimpleName();
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Task Execution Started",Toast.LENGTH_SHORT).show();
}
#Override
protected void onHandleWork(#NonNull Intent intent) {
// Write code here..
Log.i(TAG,"onHandleWork(), Thread name:" + Thread.currentThread().getName());
int duration = intent.getIntExtra("sleepTime",-1);
int ctr = 1;
//Dummy long operation
while (ctr<=duration){
Log.i(TAG,"Time elapsed " + ctr + " secs");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ctr++;
}
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"Task Execution Finished",Toast.LENGTH_SHORT).show();
}
}
Repeat : the code works well at emulator devices, pre Oreo and Oreo devices.
The log doesn't contain any related boot_completed reveiced for my application.
Thanks.
My bet is - your emulator doesn't have a passcode lock, while your real device does. BOOT_COMPLETED is broadcast only after unlocking with the passcode, and you'll have to wait for the other BroadcastReceivers to receive this event (other apps that registered for this event), depending on where you're in the queue of the system.
There's an Auto-start Manager application which was denying my app to run at startup.
Now my application works well at start.
Thanks #Arseny Levin for the hint.

Auto Start of service using BroadcastReceiver is not working?

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.

Launch Activity in android via Dialpad using Secret Code

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. :)

Service not starting after boot device

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.

Categories

Resources