I got an BroadcastReceiver which contains the following code:
package de.my.app;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent alarmIntent = new Intent(context,AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Log.e("RECEIVER", "WORKS");
}
}
In my log the log message of the Receiver shows up, but the message from the service doesn't show up.
If it would work fine it would show up every 10 seconds.
This is my AlarmService class:
package de.my.app;
import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AlarmService extends IntentService {
public AlarmService(
) {
super("AlarmService");
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onHandleIntent(Intent intent) {
Log.e("SERVICE","WORKS");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY_COMPATIBILITY;
}
#Override
public void onDestroy() {
super.onDestroy();
sendBroadcast(new Intent("restart"));
}
}
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.my.app">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<permission
android:name="de.my.app.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="de.my.app.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="Einstellungen">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="de.my.app.MyActivity" />
</activity>
<activity
android:name=".Start"
android:label="#string/app_name" />
<receiver
android:name=".GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.hmkcode.android.gcm" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="restart" />
</intent-filter>
</receiver>
<service android:name=".AlarmService" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
Instead of using getBroadcast(...) in the following line...
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
...you should be using getService(...).
Simply put - if you want a PendingIntent to be used for an Activity, Service or a Broadcast, you need to use the correct method depending on which type of app component it is.
Try...
PendingIntent pendingIntent = PendingIntent.getService(context, 0, alarmIntent, 0);
i am doing this way
//class ScheduledServiceDemoActivity
public class ScheduledServiceDemoActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PollReceiver.scheduleAlarms(this);
Toast.makeText(this, R.string.alarms_scheduled, Toast.LENGTH_LONG)
.show();
finish();
}
}
//class PollReceiver
public class PollReceiver extends BroadcastReceiver {
private static final int PERIOD=30000; // 30sec
private static final int INITIAL_DELAY=5000; // 5 seconds
#Override
public void onReceive(Context ctxt, Intent i) {
if (i.getAction() == null) {
WakefulIntentService.sendWakefulWork(ctxt, ScheduledService.class);
}
else {
scheduleAlarms(ctxt);
}
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr=
(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, PollReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP,
SystemClock.elapsedRealtime() + INITIAL_DELAY,
PERIOD, pi);
}
}
//class ScheduledService
public class ScheduledService extends WakefulIntentService {
public ScheduledService() {
super("ScheduledService");
}
#Override
protected void doWakefulWork(Intent intent) {
Log.d(getClass().getSimpleName(), "I ran!"); // will run after 30 sec
}
}
//mainfest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name="ScheduledServiceDemoActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="PollReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name="ScheduledService">
</service>
</application>
Related
I am developing android application which need auto restart when restart device for that I am using broadcast receiver with action BOOT_COMPLETED.
Broadcast receiver is receiving message when I am restarting device but in restart method I want to start main activity for that I used Intent but in onReceive method of receiver I am getting null context so I am unable to restart main Activity.
Below is code for that.
MainActivity.java
private Object activity;
private TextView tvImeiNum;
private BroadcastReceiver rebootreceiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvImeiNum = (TextView)findViewById(R.id.tv_imeinum);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
filter.addAction(Intent.ACTION_REBOOT);
rebootreceiver = new BootUpReceiver(MainActivity.this);
LocalBroadcastManager.getInstance(this).registerReceiver(rebootreceiver,
filter);//registering receiver
generateUniqueCode();
}
BootupReceiver.java
public class BootUpReceiver extends BroadcastReceiver {
MainActivity ma;
public BootUpReceiver(MainActivity maContext){
ma=maContext;
}
public BootUpReceiver(){
}
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Log.d("TAG REBOOT", "onReceive: " + intent);
Log.d("Reboot complete", "connection");
GlobalTool.restartApplication(context);
}
}
}
GlobalTool.java
public class GlobalTool {
#RequiresApi(api = Build.VERSION_CODES.M)
public static void restartApplication(Context context) {
Log.d("IN App restart:", "");
Log.d("TAG", "restartApplication: ");
if(context != null)
{
Log.d("TAG NULL", "restartApplication: ");
Intent mainIntent = new Intent(context, MainActivity.class);
AlarmManager alarmMgr = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(mainIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
long alarmTime = System.currentTimeMillis() + (1 * 1000);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.d("TAG", "restartApplication: 111");
Log.d("TAG", "restartApplication: 111");
}
else {
Intent mainIntent = new Intent(context, MainActivity.class);
AlarmManager alarmMgr = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(mainIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
long alarmTime = System.currentTimeMillis() + (1 * 1000);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.d("TAG", "restartApplication: 111");
Log.d("TAG", "restartApplication: 111");
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.kioskappdemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="KioskApplication"
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.KioskAppDemo"
tools:targetApi="31"
tools:ignore="Instantiatable">
<service
android:name=".RebootService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Activity.BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter >
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action
android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.reboot.test" />
<action
android:name="android.intent.action.TIMEZONE_CHANGED" />
<action
android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Some you code improvements:
Don't put context to Brodcast Receiver constructor. Use the Context from onReceive method
public class BootUpReceiver extends BroadcastReceiver {
public BootUpReceiver(){
//TODO log
}
...
You should not use LocalBroadcastManager::registerReceiver call. If Broadcast receiver is added to AndroidManifest then it will be started by the system.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".activity.BootUpReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Don't use CamelCase for package names (use .activity.BootUpReceiver insetad .Activity.BootUpReceiver)
System tested: Android 6.0 Device Alcatel POP4.
I have a task - run parse function white app is run, suspended, exited and device reboot.
Issue: when I reboot device the function works only once, and then stops. If I reopen app, then close it, the function will work again in background service. If I reboot device it will be called just once again. No errors given =(
Need help!
public class ParseIntentService extends IntentService {
MainActivity main = new MainActivity();
Context context;
public ParseIntentService() {
super("ParseIntentService");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
#Override
protected void onHandleIntent(Intent intent) {
main.serviceRun(this);
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}
public class NotificationAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 10000;
public static final String ACTION = "com.falcode.gratsme";
public NotificationAlarmReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, ParseIntentService.class);
context.startService(i);
}
}
public class BootBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Launch the specified service when this message is received
Intent startServiceIntent = new Intent(context, ParseIntentService.class);
startWakefulService(context, startServiceIntent);
}
}
public void startAlarm() {
Intent intent = new Intent(getApplicationContext(), NotificationAlarmReceiver.class);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, NotificationAlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // alarm is set right away
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
REQUEST_PERIOD, pIntent);
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.falcode.sovkombank">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ParseIntentService"
android:enabled="true"
android:exported="false" />
<receiver
android:name=".NotificationAlarmReceiver"
android:process=":remote" />
<receiver
android:name=".BootBroadcastReceiver"
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>
</application>
</manifest>
I am trying to follow the example given here on running some code in fixed time intervals. I used the code in this example (setting the waiting time to 1 minute instead), compiled it, started in on my phone, stopped the application on the phone, and waited for several minutes.
However, no Toast was shown and neither any log-entry. I also notice that the Service.onStart seems depreciated.
Is something missing in the example? Do I need to register something when the main activity of the app is started? Or call it from somewhere else in my app? Or maybe something is wrong in the manifest?
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.test" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application>
android:allowBackup="true"
android:icon="#mipmap/stoxx"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.Light" >
<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>
<activity
android:name=".NewEntryActivity"
android:label="#string/menu_add"
android:theme="#style/AppTheme.NoActionBar"
/>
<activity
android:name=".UserSettingActivity"
android:label="#string/menu_add"
android:theme="#style/AppTheme.NoActionBar"
/>
<receiver
android:process=":remote"
android:name=".Alarm">
</receiver>
</application>
</manifest>
You forgot to include service in your Manifest. Here is all this project that works for me, check it.
Here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager.just_me.alarmmanager">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:allowBackup="true" android:label="#string/app_name"
android:icon="#mipmap/ic_launcher" android:supportsRtl="true"
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:process=":remote" android:name=".Alarm"/>
<service android:name=".MyService"/>
</application>
Here is Alarm:
public class Alarm extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Toast.makeText(context, "com.alarmmanager.just_me.alarmmanager.Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi); // Millisec * Second
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}}
Here is service:
public class MyService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d("!~!", "Service started.");
alarm.SetAlarm(this);
return START_STICKY;
}
#Override
public void onStart(Intent intent, int startId)
{
alarm.SetAlarm(this);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
And the last one MainActivity:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
}
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);
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);