Broadcastreceiver not working after boot - android

I have created an apps that will receive notifications from firebase thus hoping to start the app service after user boot their phone so that they do not need to manually start the apps again. However, the broadcast receiver seems to just not working.
AndroidManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.simplifiedcoding.firebasecloudmessaging">
<!-- Adding Internet Permission -->
<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: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>
<!--
Defining Services
-->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".NotificationService"/>
<receiver android:name=".Broadcast" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Broadcast.java (BroadcastReceiver)
public class Broadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
Intent service = new Intent(context, NotificationService.class);
context.startService(service);
Toast.makeText(context, "Broadcast started", Toast.LENGTH_LONG).show();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have checked the followings
Permission RECEIVE_BOOT_COMPLETED declared and not within tag
receiver tag is being written correctly
Am I doing wrong or still missing something else? Do I have to call it at mainAcitivity which I doubt I should ? Any guidance are much appreciated.

Got the solution and it was a very dumb reason.
I'm using Oppo's phone to test and Oppo has its own Security Manager App where you have to manually allow specific apps to be start up automatically after boot. That's all.
I suspect most android phones has this feature as well thus bear in mind to check whether there is such app and if it does then remind the user to allow the apps in the Security Manager App before they start rebooting their phone and not able to use any service's your App intended to provide.
Hope this helps!

Try this, it worked for me.
<receiver android:enabled="true" android:name=".Broadcast"
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>

Related

Android: How to start a service on boot completed of MI devices?

In an application, I am trying to implement a service which needs to run every time when device boot process completed.
Application unable to broadcast service only in MI devices.
Manifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
BroadcastReceiver
public class SavedLocationBroadcast extends WakefulBroadcastReceiver {
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onReceive(Context context, Intent intent) {
App.showToast(context,"BROADCAST","SEND");
Intent service = new Intent(App.getContext(), MyLocationService.class);
startWakefulService(App.getContext(), service);
}}
Reciever in manifest
<receiver android:name="com.geolocation.broadcastreciever.SavedLocationBroadcast">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="com.geolocation.app.ACTION_PULSE_SERVER_ALARM" />
</intent-filter>
</receiver>
Please share your suggestion is anything wrong in implementation.
This is solution for your bug.
What you need to do is that , you need to mention this permission also in your Intent-Filter.
<receiver android:name="com.geolocation.broadcastreciever.SavedLocationBroadcast"
android:enabled="true"
android:exported="true" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
Here you need to do one thing also.
Go to "Settings-Apps_open your app info-Manage permisson" in yourdevice
Here check all things here.
Because for some devices it will not work. Ex.Red mi
Try this !

Android - BroadcastReceiver Not Triggering for BOOT_COMPLETED

I am working with a Samsung Galaxy Grand Prime that uses Android 5.0.2. What I want to do is simple, I want my application to boot up when the phone restarts. I am aware that to activate broadcasters I must run the application manually first after installing it. I do this and it still doesn't work. Here's my broadcaster code:
public class onStartupReceiver extends BroadcastReceiver {
public onStartupReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Log.e("PLEASE", "System boot notification received.");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("PLEASE", "System boot notification received.");
Intent i = new Intent(context,TestGPSBackgroundActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
And here's my manifest relevant code:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:installLocation="internalOnly">
<activity
android:name=".TestGPSBackgroundActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.example.jorge.gpsevixel.onStartupReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
I've been doing a lot of research that suggests removing the line android:permission="android.permission.RECEIVE_BOOT_COMPLETED" from the receiver and such, but no luck.
I've read things like App doesn't auto-start an app when booting the device in Android and a lot of similar questions/answers but none has helped me. Hope Anyone can help me I"m getting desperate.

My broadcast receiver can't get BOOT_COMPLETED broadcast

I install and open it , and reboot my phone , but my receiver did't receive broadcast to start my service and without log.
My phone is Asus LF2 .
How can I start my service in device boot completed?
My Receiver
public class BootReceiver extends BroadcastReceiver {
private final String TAG = getClass().getSimpleName();
public BootReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG , "Deyu onReceive " + intent.getAction());
context.startService(new Intent(context, AlarmMessageService.class));
}
}
My manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="go.deyu.dailytodo"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".app.App"
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>
<receiver
android:name=".receiver.BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service
android:name=".AlarmMessageService"
android:enabled="true">
</service>
</application>
</manifest>
I find why my app in my phone can't get boot complete broadcast.
There is a Auto-start Manager setting in my Asus Phone.
When I allow my app to Auto-start , my App work fine....
Remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED" from the <receiver> element.
Try below code
<receiver
android:name=".receivers.RestartReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Android 3.1 or above,if you want to deal with android.intent.action.BOOT_COMPLETED broadcastreceiver.You must pay attention to this:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
1.Make sure you have turned on your app after you installed.I think you did it.
2.Check your android mobile device settings: Settings -> Apps -> Your App -> Force Stop.If the Force Stop is turned on,please turn off it.
3.Another point you should check is android:name=".receiver.BootReceiver",be careful of the path,maybe system can not find your BootReceiver.

NFC broadcast problem

I really read 10 or 20 topics about that and unfortunately I didn't make it working. My receiver can capture broadcast but only if I send it via sendBroadcast(intent) from my app. I want it to capture broadcast from NFC adapter. F.e someone puts NFC Tag near my device and then my app should start or show in browsing menu, however that doesn't happen. Even if my app starts, and I put NFC Tag near device, it can't capture it, and in browsing menu I see other apps, which can.
My receiver:
public class SomeBroadcastReceiver extends BroadcastReceiver {
private final String TAG = "SomeBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Got intent: " + intent);
}
}
And my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nfc">
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<receiver android:enabled="true" android:name=".broadcast.SomeBroadcastReceiver">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="#xml/technologies"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
</receiver>
<activity android:name=".simulator.FakeTagsActivity"
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>
<activity android:name="TagViewer"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="10" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
In FakeActivity i have this lines:
Intent exampleIntent = new Intent("android.nfc.action.NDEF_DISCOVERED");
sendBroadcast(exampleIntent);
And when app reches them, my receiver captures intent, so I think that receiver is fine, but maybe I miss something in manifest? Are there special permission to capture global broadcast? Or should I start service or sth?
You can't capture those intents with a BroadcastReceiver, because only Activities can receive NFC intents. You can find more information about it in the NFC guide.
I use technique described in this answer - it provides the same effect as a broadcast receiver.
According to here (https://stackoverflow.com/a/5320694/3736955), you cant catch NFC intent by BroadcastReceiver. The only way to handle it is by ForegroundDispatch and onNewIntent() function within activity. When NFC Tag is tapped, it looks for foreground activity to handle him.

Android: Start Service on boot?

I'm reaaaaally new to Java, but an experienced C#-coder.
I've created a service which I can start/stop from an activity.
My question is, how do I "install" this service so it does start upon boot of my device?
I found this:
Trying to start a service on boot on Android
I've tried to implemented this like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="james.jamesspackage" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".jamessActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".MyService">
<intent-filter>
<action android:name="james.jamesspackage.MyService" />
</intent-filter>
</service>
<receiver android:name="james.jamesspackage.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
</manifest>
What's wrong? Can I have an activity and a service/receiver in one manifest?
Thanks
James
How to start service on device boot(autorun app, etc.)
For first: since version Android 3.1+ you don't recieve BOOT_COMPLETE if user never started yor app at least once or user "force closed" application.
This was done to prevent malware automaticaly register service. This security hole was closed in newer versions of Android.
Solution:
Create app with activity. When user run it once app can recieve BOOT_COMPLETE broadcast message.
For second: BOOT_COMPLETE is sent before external storage is mounted. if app is installed to external storage it won't receive BOOT_COMPLETE broadcast message.
In this case there is two solution:
Install your app to internal storage
Instal another small app in internal storage. This app recieves BOOT_COMPLETE and run second app on external storage.
If your app already installed in internal storage then code below can help you understand how to start service on device boot.
In Manifest.xml
Permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Register your BOOT_COMPLETED reciever:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Register your service:
<service android:name="org.yourapp.YourCoolService" />
In reciever OnBoot.java:
public class OnBoot extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// Create Intent
Intent serviceIntent = new Intent(context, YourCoolService.class);
// Start service
context.startService(serviceIntent);
}
}
For HTC you maybe need also add in Manifest this code if device don't catch RECEIVE_BOOT_COMPLETED:
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
Reciever now look like this:
<receiver android:name="org.yourapp.OnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
How to test BOOT_COMPLETED without restart emulator or real device?
It's easy. Try this:
adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED
How to get device id? Get list of connected devices with id's:
adb devices
adb in ADT by default you can find in:
adt-installation-dir/sdk/platform-tools
Enjoy! )
Looks like the name in the receiver section is wrong. This is what my application entry in the AndroidManifest.xml looks like:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".BootListener"
android:enabled="true"
android:exported="false"
android:label="BootListener">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".UpdateService">
</service>
<activity android:name=".Info"
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=".TravelMapperPreferences"
android:label="Settings">
</activity>
</application>
Note that the names are relative to the package in the manifest declaration. Your receiver name should be ".MyBroadcastReceiver" since the package of the manifest contains james.jamesspackage

Categories

Resources