Android boot start app on boot from BroadcastReceiver with multiple services - android

I need to start 2 services on bootcompleted. The first service starts correctly, but second service seems is not starting.
I don't know if I have to create two BroadcastReceiver or it's enough with one.
Here is my code. I've put the two services in one BroadcastReceiver. Please, can you tell me what I'm doing wrong?
Thank you in advance
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pruebas.appservicelocator"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<!-- Startup service -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- GPS -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- UUID -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- Acceso a web service -->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.pruebas.appservicelocator.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>
<service android:name=".Servicio">
<intent-filter>
<action android:name="com.pruebas.appservicelocator.Servicio"/>
</intent-filter>
</service>
<service android:name=".ServicioBD">
<intent-filter>
<action android:name="com.pruebas.appservicelocator.ServicioBD"/>
</intent-filter>
</service>
<receiver android:name=".Recibidor" 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>
</application>
</manifest>
Recibidor.java:
package com.pruebas.appservicelocator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class Recibidor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
android.os.Debug.waitForDebugger();
Toast.makeText(context, "Iniciando Recibidor", Toast.LENGTH_LONG).show();
final String TAG = "Recibidor";
Log.i(TAG, "Iniciando Recibidor");
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "Iniciando Intent", Toast.LENGTH_LONG).show();
Log.i(TAG, "Iniciando Intent");
Intent servicio = new Intent();
servicio.setAction("com.pruebas.appservicelocator.Servicio");
context.startService(servicio);
Intent servicioBD = new Intent();
servicio.setAction("com.pruebas.appservicelocator.ServicioBD");
context.startService(servicioBD);
Log.i(TAG, "Iniciando Servicios");
Toast.makeText(context, "Iniciando Servicio", Toast.LENGTH_LONG).show();
}
}
}
"Servicio" service works fine, so I don't write the code. If you need, please, tell-me and I will write it.
ServicioBD.java:
package com.pruebas.appservicelocator;
import com.pruebas.utils.UsersLocationsDBHelper;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class ServicioBD extends Service{
private UsersLocationsDBHelper locDBHelper = null;
private static String TAG = "ServicioBD";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "SERVICIOBD ON CREATE", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(0, null);
Toast.makeText(this, "SERVICIOBD ON START COMMAND", Toast.LENGTH_LONG).show();
return START_STICKY;
}
}

I've found the error.
Intent servicio = new Intent();
servicio.setAction("com.pruebas.appservicelocator.Servicio");
context.startService(servicio);
Intent servicioBD = new Intent();
servicio.setAction("com.pruebas.appservicelocator.ServicioBD");
context.startService(servicioBD);
Has to be
Intent servicio = new Intent();
servicio.setAction("com.pruebas.appservicelocator.Servicio");
context.startService(servicio);
Intent servicioBD = new Intent();
servicioBD.setAction("com.pruebas.appservicelocator.ServicioBD");
context.startService(servicioBD);

Related

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.

How to start an app at startup on Android?

I've tried:
This which doesn't work on my phone:
Trying to start a service on boot on Android
This which also fails to work properly:
http://www.compiletimeerror.com/2014/12/android-autostart-app-after-boot-with.html#.VpL6sxWLTIU
And a few others which I couldn't find again. Could someone please post a working example of code which will start a MainActivity with the default HelloWorld xml layout?
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".MyService"
android:label="#string/title_activity_my_service" >
</activity>
</application>
</manifest>
package com.example;
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) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MyService extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_service);
}
}
First:create a BroadcastReceiver, onReceive(Context context, Intent intent),start the app you want to startup in this method
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
xxx.class is the app you want to start
Intent service = new Intent(context,XXXclass);
context.startService(service);
Log.v("TAG", "you want to startup this app.....");
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
context.startActivity(intent );
} }
Secondly:configure receiver and intent-filter
<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
THirdly,add permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

I don't receive the RECEIVE_BOOT_COMPLETED in the Emulator

I'm French sorry for my English. I'm trying to develop an autoboot service that runs in the background and catches SMS. After a lot of research, I wasn't able to solve my problem.
I can't run the service after the boot and I don't receive the log
Log.v("LTM","MyReceiver.onReceive: "+intent.getAction());
that should to be printed by the BootReceiver. (CF Code below)
I verified the AndroidManifest, tested different code, and nothing works right. I use Eclipse and test the code with the Android Virtual Devices, so maybe the problem comes from the Emulator and I tested with a real device but it didn't start too.
You can find the code below. Thank you for your help.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smsmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:enabled="true" android:label="ServiceGUI" android:name="com.smsmanager.MainService"/>
<receiver android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver class="com.smsmanager.SMSReceiver" android:name="com.smsmanager.SMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Main:
package com.smsmanager;
import com.smsmanager.SMSReceiver;
import android.os.IBinder;
import android.util.Log;
import android.app.Service;
import android.content.Intent;
public class MainService extends Service {
SMSReceiver rec;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("LTM", "Demarrage du service");
rec=new SMSReceiver();
Log.v("LTM", "Retour dans le service");
return super.onStartCommand(intent, flags, startId);
}
}
Receiver:
package com.smsmanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
// TODO Auto-generated method stub
Log.v("LTM","MyReceiver.onReceive: "+intent.getAction());
Intent intent1=new Intent(ctx,MainService.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startService(intent1);
}
}

Starting a service that start an activity on device boot

im trying to start an application on device boot.
my code is as follow
1- firstly the main class which contain a background thread (asynctask) that send data to a mysql database.
2- Service class
package com.seven.ex.helper;
import com.seven.ex.AndroidGPSTrackingActivity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class service extends Service {
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),AndroidGPSTrackingActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
3- BootUpReceiver class
package com.seven.ex.helper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent arg1) {
Intent intent = new Intent(arg0,service.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
}
4- the android manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpstracking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.seven.gpstracking.AndroidGPSTrackingActivity"
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:name=".service"
android:label="#string/app_name"
>
</service>
<receiver android:enabled="true" android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
The main method is working correctly before i tried to start it on boot.
Also the app still working after i ignore the android error on boot (The App Closed)
in the log cat am getting a class not found exception
can you please help ?
Your Service won't work like it is at the moment. You will have to move the stuff from onStart() to onStartCommand() and then return whether the service is sticky or not. The problem is that the onStart()-method has a good chance of not getting called at all (as it is deprectaed by now).
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
//do your stuff here
return Service.START_NOT_STICKY;
}
In your manifest you have assigned package: com.example.gpstracking. When u then define .BootUpReceiver. The system should expect to locate the class at com.example.gpstracking.BootUpReceiver.
Please try and change the package .BootUpReceiver to the full path com.seven.ex.helper.BootUpReceiver. The same goes for AndroidGPSTrackingActivity as this should be com.seven.ex.AndroidGPSTrackingActivity as far as I can tell.

start my service on phone restart in android

I am making app which tracks user location continuously, so far i have been able to make a successful receiving of its ordinate on location change, but if he is restarting the phone than i am not able to start my service without user again opening the app.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aa.gpsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.aa.gpsdemo.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>
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.aa.gpsdemo.StartMyServiceAtBootReciever" />
</intent-filter>
</service>
<receiver
android:name=".receiver.StartMyServiceAtBootReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
StartMyServiceAtBootReciever.java
package com.aa.gpsdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartMyServiceAtBootReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent serviceIntent = new Intent("com.aa.gpsdemo.BackgroundService");
context.startService(serviceIntent);
}
}
}
BackgroundService.java
package com.aa.gpsdemo;
import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.IBinder;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;
import android.location.Criteria;
public class BackgroundService extends Service {
private static NewLocationListener mylistner=null;
private final static String TAG = "MainActivity";
private Context context = null;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
mylistner = new NewLocationListener(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
criteria.setCostAllowed(true);
LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
String bestProvider=locManager.getBestProvider(criteria, true);
locManager.requestLocationUpdates(bestProvider,1000, 5, mylistner);
}
}
where i am going wrong any help would be highly appreciated on restart of a phone i get demoapp has stopped i tried putting toast on reciever but that toast also are not coming at present
Make Sure u have the permission in AndroidManifest File :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Change this to :
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context,BackgroundService.class);
startService(serviceIntent);
}
RECEIVE_BOOT_COMPLETED: Broadcast Action: This is broadcast once, after the system has finished booting. It can be used to perform application-specific initialization, such as installing alarms. You must hold the RECEIVE_BOOT_COMPLETED permission in order to receive this broadcast.
Refrence
Stackoverflow Question

Categories

Resources