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
Related
I am simply trying to implement the AlarmManager. I wrote the code for alarm manager but the code doesnot work. AlarmManager doesnot fire the Broadcast Receiver and service. But when I donot use the AlarmManager and simply start the service using intent the service run. How to make AlarmManager work to schedule the service periodically?
Below is the code:
MainActivity.java
package com.alarmmanager;
import android.app.*;
import android.os.Bundle;
import android.content.*;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),15000,pending);
}
}
AlarmReceiver.java
package com.alarmmanager;
import android.content.*;
public class AlarmReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
intent =new Intent(context,MainService.class);
context.startService(intent);
}
}
MainService.java
package com.alarmmanager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MainService extends Service
{
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
Log.i("nilavs","nilav");
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".MainService"
android:enabled="true"
/>
<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:process=":remote" android:name=".AlarmReceiver">
</receiver>
</application>
</manifest>
You should use getBroadcast instead of getService?
PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);
Edit:
Try setting it to fire off in the future initially - e.g.
alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+5000,15000,pending);
below codes are IntentService , BoradCastReceiver , MainAcitivity, Menifest
Actually When the app is started IntentService is called from MainActivity
When called from MainActivity, it works fine.
But When called from BroadCastReceiver, it seems not called.
Please tell me what i should do to solve it.
Menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.location"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_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.example.location.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=".backgraound.LocationBackGround"
android:exported="true" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyA_49-k8bdNVqMJkMTtl3hU97No3poJBzs" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<uses-library
android:name="com.google.android.maps"
android:required="true" />
<receiver
android:name=".backgraound.AlarmReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
<receiver
android:name=".backgraound.startBackGround"
android:enabled="true" >
</receiver>
</application>
</manifest>
BroadCastReceiver
package com.example.location.backgraound;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class startBackGround extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast. makeText( context , "startBackGround", Toast.LENGTH_SHORT ).show();
Intent mServiceIntent = new Intent(context, LocationBackGround.class);
context.startService(mServiceIntent);
}
}
IntentService
package com.example.location.backgraound;
import java.util.Iterator;
import java.util.List;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.example.location.db.MySQLiteHelper;
import com.example.location.meta.LocationNState;
public class LocationBackGround extends IntentService {
private MySQLiteHelper db = null;
private List<LocationNState> locationNStates = null;
private Context context = null;
private LocationManager mLocMan = null;
private Location currentLocation = null;
private AudioManager mAudioManager = null;
public LocationBackGround() {
super("LocationBackGround");
// TODO Auto-generated constructor stub
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
context = getBaseContext();
Toast.makeText( context , "intentService start : LocationBackGround", Toast.LENGTH_SHORT ).show();
db = new MySQLiteHelper(context);
locationNStates = db.getAllLocationNStates();
mLocMan = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
requestLocation();
}
}
When i boot up then i could see a Taost showing "StartBackGround".
But i cannot see "intentService start : LocationBackGround" Toast
is there any problem with my code?
Please teach me...
your code looks good. I use a very similar code with my service after booting the device. The difference in my project is that I call a Service - not an IntentService.
Maybe this will help you: http://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183
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);
}
}
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.
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);