Android app crashes on Device Boot - android

I am working on an Application that requires to start a BackgroundService on Android Boot
This is the code that I am using to start the BroadcastReceiver on Android Boot
public class StartOnBootService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try
{
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.package.myApplicationPackage.BackgroundService");
context.startService(serviceIntent);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This is my BackgroundService.class
public class BackgroundService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
//code to execute when the service is first created
Toast.makeText(getBaseContext(), "BACKGROUND SERVICE STARTED", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
//code to execute when the service is shutting down
}
#Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
}
}
This is the error log that I managed to snipe from the CatLog while my Android was Booting.
12-21 10:28:01.279: E/EmbeddedLogger(1710): App crashed! Process: com.package.myApplicationPackage
12-21 10:28:01.289: E/EmbeddedLogger(1710): App crashed! Package: com.package.myApplicationPackage v1 (1.0)
12-21 10:28:01.289: E/EmbeddedLogger(1710): Application Label: myApp Label
My AndroidManifest.xml file
</service>
<service android:name=".BackgroundService">
<intent-filter>
<action android:name="com.package.myApplicationPackage.BackgroundService" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartOnBootService"
android:enabled="true"
android:exported="true"
android:label="StartOnBootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

You Broadcast receiver name and android manifest receiver name is totally differ.
StartOnBootService or in manifest it is StartMyServiceAtBootReceiver

Maybe it's a good practice to look at the manifest file before rushing over stackoverflow.
I also had a similar problem, "crash after boot" and noticed that I had a wrong package name, as a result of a hasty copy paste action for one of my receivers.
<receiver android:name="com.somepackage.broadcast.ConnectionChangeReceiver" />
was indeed having a typo as
<receiver android:name="com.somepackage.broadcast.broadcast.ConnectionChangeReceiver" />
Correcting it obviously solved my problem.

Related

starting background service while phone turn on with example code

I need an app which will run always in the background and apps will start while phone is turn on. Please help me with example code.
I already tried several code but it run on background while pressing button after start the apps
You need to receive BOOT_COMPLETED of the phone, then start the service.
Follow the following steps
Step 1: create your service
public class myService extends Service{
public myService(){}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
}
Step 2: Create your boot receiver
public class BootReceiver extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent(context, RemindersService.class);
context.startService(i);
}
}
Step 3: add them to manifest inside application
<service
android:name=".services.RemindersService"
android:enabled="true"
android:exported="true" />
<receiver
android:name=".services.BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Step 4: add permission in manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Thats it. Happy coding.
Please note that in android Oreo, you will want to start service as foreground
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(i);
}

why Broadcast Receiver Don't work for service application android?

I have a project which is only a service and it has no activity and user interface. I want to start my application background service when phone boot completely. but I never receive the "BOOT_COMPLETED" Message from OS. these are my code:
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droid.arghaman.location_tracker">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</receiver>
</application>
<service android:name=".mySevice"></service>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
Broadcast Receiver:
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("boot Received", intent.getAction());
Intent serviceLuncher = new Intent(context, myService.class);
context.startService(serviceLuncher);
}
}
myService:
public class LocationNotifierService extends Service {
Timer timer ;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate(){
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Toast.makeText(getBaseContext(),"Location",Toast.LENGTH_SHORT).show();
}
},3000);
}
#Override
public void onDestroy(){
}
#Override
public int onStartCommand(Intent intent, int flagId, int startId){
return START_STICKY;
}
}
but I never get "boot Received" log.
is there any mistake and is there any way to debug my program?
I Recommend that my project must have only this Service and it cannot have any UI.
I never receive the "BOOT_COMPLETED" Message from OS
Partly, that is because you do not have a <receiver> set up to receive android.intent.action.BOOT_COMPLETED broadcasts.
Partly, that is because your app will not receive broadcasts until something on the device uses an explicit Intent to start one of your components. The way your app is set up — without an activity that the user can run — it is unlikely that any app will do this, and so your code will never run.
Also, please bear in mind that Android O has changes designed specifically to prevent background services from running for very long and to limit your ability to get background location updates (which your location_tracker name suggests that you want to add in the future). You may wish to reconsider whether writing this app the way that you are is a wise course.
try this in your manifest
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:exported="false"
android:label="StartServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Why do dynamically register broadcast receiver didn't work after reboot

I have a onBootCompleted broadcast receiver registered in the manifest.
It runs starts MyService. My service in the onCreate registers 3 more broadcast receivers dynamically.
The 3 new receivers filter on the following intent actions
LOCALE_CHANGED,
TIMEZONE_CHANGED and
CONNECTIVITY_CHANGED.
These works correctly when I run the application from Eclipse but, after I reboot the device and my service starts up none of receivers work.
I have a work around implementation but, I would like to know why this is happening?
Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".receiver.BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="false"
android:stopWithTask="false" >
</service>
Service:
public class MyService()
{
LocationTimeZoneC‌​hangedReceiver mLocationTimeZoneC‌​hangedReceiver = new LocationTimeZoneC‌​hangedReceiver()
NetworkChangedReceiver mNetworkChangedReceiver = new NetworkChangedReceiver()
public void onCreate()
{
registerReceiver(mLocationTimeZoneChangedReceiver, new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
registerReceiver(mLocationTimeZoneC‌​hangedReceiver, new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED));
registerReceiver(mNetworkChangedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
BootCompletedReceiver:
public class BootCompletedReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent){}
}
MyApplication:
public class MyApplication extends Application
{
ServiceConnection mServiceConnection = new ServiceConnection() { anonymous class...}
public void onCreate()
{
bindService(new Intent(this, MyService.class), mServiceConnection,Context.BIND_AUTO_CREATE);
}
}
Edited:
Edited code for Plinio.Santos.
It's a big app with many moving parts so at best I can post small code snippets.
Following are the steps I am following for testing:
Push app via Eclipse,
test that network change receiver is working
leave wifi off
Now restart the device
wait for the process to start and turn on wifi.
I believe that the service is not started or bound due errors. Unfortunately I can not say it for sure without all binding/starting code.
Anyway, you can see bellow a code that worked fine after I rebooted (the app started, registered the receiver and is receiving the CONNECTIVITY_CHANGED broadcast.
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".TestReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".TestService"
android:exported="true" />
Receiver class:
public class TestReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Toast.makeText(context, "Intent.ACTION_BOOT_COMPLETED receiverd !", Toast.LENGTH_SHORT).show();
context.startService(new Intent(context, TestService.class));
}
}
}
Service class:
public class TestService extends Service {
private BroadcastReceiver mConnectivityChangedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null && ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
Toast.makeText(context, "ConnectivityManager.CONNECTIVITY_ACTION receiverd !", Toast.LENGTH_SHORT).show();
}
}
};
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
registerReceiver(mConnectivityChangedReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}

How to start an activity via a service after rebooting

I need my app to start running (in the background) after rebooting the device. Here's what I've come up with till now (after taking a lot of help from here...)
This is my BootUpReceiver making use of broadcastreceiver:
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, RebootService.class);
serviceIntent.putExtra("caller", "RebootReceiver");
context.startService(serviceIntent);
}
}
This is the service class:
public class RebootService extends IntentService{
public RebootService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
protected void onHandleIntent(Intent intent) {
Intent i = new Intent(getBaseContext(), MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String intentType = intent.getExtras().getString("caller");
if(intentType == null)
return;
if(intentType.equals("RebootReceiver"))
getApplication().startActivity(i);
}
}
THis is my android manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".BootUpReceiver"
android:enabled="true"
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=".RebootService"/>
</application>
The problem is, When I install this on my phone and reboot, the app crashes: It says, "Transfer has stopped working". After I press the OK button, when I check the app info, the app is running.
I'm new to android and I'm not sure what is going on. Am I supposed to add any more permissions?
Kindly help.
TIA
I think your problem lies with your RebootService constructor. When the system invokes it, it doesn't provide any arguments, so it's going to crash. If you look in the logs you'll probably see something to the effect of "Unable to instantiate service..."
Try replacing your constructor with:
public RebootService() {
super( "Reboot Service" );
}

Start app automatically

I want to start my Android app automatically on following cases :
Reboot
App were stoped throug TaskManager or Ram clearing
I search on web and found stuff about Android-Services.
I implement some code for start my service on reboot, but i doesn't work, and i have no idea how to get informed about case 2.
Can you give me an advice?
This is my code for Service:
on manifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name="TimetableService">
<intent-filter>
<action android:name=".TimetableService" />
</intent-filter>
</service>
<receiver android:enabled="true" android:name=".OnBootReceiver" 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>
OnBootReceiver :
public void onReceive(Context context, Intent intent)
{
Intent serviceIntent = new Intent();
serviceIntent.setAction("TimetableService");
context.startService(serviceIntent);
}
TimetableService :
public class TimetableService extends Service
{
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
I would look into using the AlarmManager to schedule your app to run at a specific time of the day. That way it doesn't matter if something closes your app down, the alarm manager will wake it up again with an intent at the time you specify.
Lets use service to auto start app
Now when boot completes you can use the following code to restart your service.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// check for interreupted flag in shared pref
// if true restart your service
}
}
Add following code to your manifest
<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

Categories

Resources