Reputation: 4303
I am working on an android app that runs with a service. The service will get your location in the background from the simcard. I want to run the service always: every 30 seconds or something (don't need to be exact) using the alarmmanager. Also when the phone reboots the service needs to be started again.
But I can't get the service to run. This is the code I wrote for this problem:
I added the permissions to the AndroidManifest file:
<service android:enabled="true" android:name=".services.CountryService" />
<receiver android:name=".broadcasters.CountryUpdateReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I wrote a broadcast receiver that will start the service on boot completed. And I need to add something later that will start the service when you install the app.
The broadcast receiver called CountryUpdateReceiver:
public class CountryUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
CountryService.acquireStaticLock(context);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent countryIntent = new Intent(context, CountryService.class);
PendingIntent countryService = PendingIntent.getService(context, 0, countryIntent, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10 * 1000, 10 * 1000, countryService);
}
}
And last I have the service itself called CountryService:
public class CountryService extends WakefulIntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public CountryService() {
super("CountryIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("Service", "Test");
//android.os.Debug.waitForDebugger();
String country = getUserCountry(this);
Uri uri2 = Uri.parse(CountryProvider.CONTENT_URI + CountryProvider.COUNTRY_ISO_ADD);
getContentResolver().update(uri2, null, null, new String[] {"BE"});
//Intent broadcastIntent = new Intent("my-event");
// add data
//intent.putExtra("message", "data");
super.onHandleIntent(intent);
}
/**
* Get ISO 3166-1 alpha-2 country code for this device (or null if not available)
* Origin: http://stackoverflow.com/questions/3659809/where-am-i-get-country/19415296#19415296
* @param context Context reference to get the TelephonyManager instance from
* @return country code or null
*/
public static String getUserCountry(Context context) {
try {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
final String simCountry = tm.getSimCountryIso();
if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
return simCountry.toLowerCase(Locale.US);
} else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
String networkCountry = tm.getNetworkCountryIso();
if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
return networkCountry.toLowerCase(Locale.US);
}
}
} catch (Exception e) {
}
return null;
}
The service itself extends from WakefulIntentService
(Gist). This is a class written by somebody else for getting a wakelock. I followed this Example, because I didn't have any experience with services.
Upvotes: 1
Views: 180
Reputation: 161
Adding following permissions to manifest should help.
These does not seem to added to your manifest.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Upvotes: 1