Android: Broadcast receiver won't receive boot up message - android

I've a class MyBroadcastReceiver that is registered to start at boot up. The manifest is below. I am running this via eclipse on a Android Motorola Xoom wifi only tablet - which is Android 3.2, API level 13 (from the dev.android site).
I have googled for this and tried a few things:
put android:exported="true"
used android:installLocation="internalOnly" to make sure it doesn't install on the SD card
after installing on the device, I ran the app a couple of times before testing the bootup (as some links said Android 3.x onwards won't immediately start sending boot events to the app unless user has manually initiated it at least once)
However even now the broadcast receiver doesn't seem to run when the device boots up (based on logcat).
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypack"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainCamApp" 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=".UploaderService" />
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Code for broadcast receiver -
import java.util.Calendar;
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 MyBroadcastReceiverX extends BroadcastReceiver {
private String TAG = "Broadcast Receiver:";
AlarmManager alarm;
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.i(TAG, "Entered onReceive.");
alarm = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
Intent intentUploadService = new Intent (arg0, UploaderService.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
PendingIntent pi = PendingIntent.getBroadcast(arg0, 0, intentUploadService , 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
Log.i(TAG, "Alarm set. Exiting onReceive.");
Intent myIntent = new Intent(arg0, UploaderService.class);
arg0.startService(myIntent);
}
}
Seems to be working now.. final manifest below if it helps -
<?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"
android:installLocation="internalOnly"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="8" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainCamApp" 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=".UploaderService" />
<receiver android:name=".MyBroadcastReceiverX" 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"/>
</action>
</intent-filter>
</receiver>
</application>
</manifest>

Try the following:
<receiver android:name=".MyBroadcastReceiverX" 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"/>
</action>
</intent-filter>
</receiver>
You also have incorrect class name specified in manifest - it should be MyBroadcastReceiverX rather then MyBroadcastReceiver

You need to write code after receiving the android.intent.action.BOOT_COMPLETED intent.
try this.
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TEST", intent.getAction());
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.BOOT_COMPLETED")) {
Log.i(TAG, "Entered onReceive.");
alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intentUploadService = new Intent (context, UploaderService.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intentUploadService , 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5000, pi);
Log.i(TAG, "Alarm set. Exiting onReceive.");
//no need to initialize intent twice
// Intent myIntent = new Intent(context, UploaderService.class);
context.startService(intentUploadService );
}
}

Related

Android BroadcastReceiver not triggered

there are a lot of questions related to this topic. but I cannot get it to run on my Device 4.2.1 on the emulator 5.0 it is working fine.:
here the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bootService"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:label="MyBroadcastReceiver" >
<intent-filter android:priority="1001" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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>
<service android:name=".PingService" >
</service>
<service android:name=".UpdateService" >
</service>
<receiver android:name=".OnBootReceiver" >
<intent-filter android:priority="1001" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
I tried it with 2 receivers: none of them working.
Here one of the receivers that should write a logfile to SD, which is working fine from the Launcher Activity:
public class MyBroadcastReceiver extends BroadcastReceiver {
// Restart service every 30 seconds
private static final long REPEAT_TIME = 1000 * 10;
#Override
public void onReceive(Context context, Intent intent) {
Log.e("MyBroadcastReceiver", "BroadcastReceiver");
appendLog("MyBroadcastReceiver BroadcastReceiver \n");
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, UpdateService.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
Log.wtf("MyBroadcastReceiver", "Alarm went off");
}
public static void appendLog(String text) {
File logFile = new File("sdcard/logfile.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
// BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
true));
buf.append(text);
buf.newLine();
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I cannot see the error. Anyone does?
PS the app is started once, so this: http://commonsware.com/blog/2011/07/05/boot-completed-regression.html is covered.
thanks,
It took me long the key to the problem was in the manifest.xml
here the changes to got it run (only the receiver block):
<receiver
android:name="com.example.bootService.MyBroadcastReceiver"
android:exported="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data
android:path="com.example.bootService"
android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
important was the
<category android:name="android.intent.category.DEFAULT" />
tag

Android 4.4.4 Kitkat: Broadcastreceiver not receiving on_boot_completed broadcast

My program is to listen receive_boot_complete broadcast when device completes the boot.
The program is working fine as expected with android 4.0.3, but the same thing is not happening when I am running the code on Android 4.4.4 device.
The broadcastreceiver is not receiving a boot completed broadcast.
Is there anything new for Android 4.4.4? Running it on Moto G.
My code is as below:
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app"
android:versionCode="1"
android:installLocation="internalOnly"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:excludeFromRecents="true" >
<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="#string/ser_name"
android:icon="#drawable/serv_ico"
android:enabled="true" />
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".AlarmReceiver"
android:enabled="true" >
</receiver>
</application>
BootReceiver
public class BootReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context context, Intent intent) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// Start alarm manager on boot
// Start service
}
}, 60000);
}
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
// Start service
}
}
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Run service on application start
setContentView(R.layout.activity_main);
// Initialize alarm manager application start
PackageManager p = getPackageManager(); // Remove App icon from launcher
p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
}
AlarmManager
public class Alarm {
public Alarm schedule(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
return null;
}
}
You have to define your receiver with exported = true and enabled = true
<android:name=".BootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" />
This might be because of a bug in Android 4.4.4 r1/r2
See
Bug in Android KitKat

Alarmservice not working

In my application I need to run some code every midnight. This is the first time I am working on alarm service and Searched the internet for some snippets and examples.
Below is my code and its not working as expected, please help my pointing out my mistake:
SET A REPEATING ALARM
private static void initiateMidnightAlarm(Context context, AlarmManager alarmManager) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent midnightIntent = PendingIntent.getService(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, midnightIntent);
}
DECLARE THE RECEIVER IN THE MANIFEST:
<receiver
android:name=".receivers.MidnightProcessReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.alarms.MidnightProcess"/>
</intent-filter>
</receiver>
RECEIVER CODE:
public class MidnightProcessReceiver extends BroadcastReceiver {
public MidnightProcessReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
processMidnightLogic(context);
}
public static void processMidnightLogic(Context context) {
Toast.makeText(context,"MidnightProcessReceiver",Toast.LENGTH_LONG).show();
}
}
UPDATE
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appname">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<permission
android:name="com.appname.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.appname.permission.C2D_MESSAGE"/>
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
.
.
..
..
</activity>
<receiver
android:name=".receivers.MidnightProcessReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.alarms.MidnightProcess"/>
</intent-filter>
</receiver>
<receiver
android:name=".receivers.OnBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
Please help!
Thanks in advance.
Since you need to fire an implicit broadcast, rather than start a Service, you're retrieving the wrong type of PendingIntent. Change:
PendingIntent midnightIntent = PendingIntent.getService(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);
to:
PendingIntent midnightIntent = PendingIntent.getBroadcast(context, 0,
new Intent("com.alarms.MidnightProcess"), 0);

Autostart app immediately after the boot

I want to start my application when phone startup
I just follow tutorial from here but it doesn't work in my device. Please see my method:
package net.londatiga.android;
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, ExampleActivity.class);
context.startService(startServiceIntent);
}
}
And this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.londatiga.android"
android:versionCode="2" android:versionName="1.01">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="net.londatiga.android.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".ExampleActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Where is my mistake please?
Instead of:
context.startService(startServiceIntent);
Use:
context.startActivity(startServiceIntent);
You don't have any Service, You need to open activity.
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startActivity(startServiceIntent);
Create a class name it as AfterBootActivity:
public class AfterBootActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
Now, Create a new class named as Autostart.java which extends a BroadcastReceiver:
public class Autostart extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, StarterService.class);
context.startService(intent);
}}
In the Manifest file add this class as a receiver. This class will listen to the Broadcast call the Android OS sends after the boot sequence has finished i.e. after the phone started up.
Now Create a class named as StarterService.java which will extend Service:
public class StarterService extends Service {
private static final String TAG = "MyService";
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
/**
* The below started service opens the Activity.
*/
public void onStart(Intent intent, int startid) {
Intent intents = new Intent(getBaseContext(), AfterBootActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}}
When the class Autostart receives the BOOT_COMPLETED Broadcast from Android OS it will start the StarterService which then starts the Android Activity “AfterBootActivity” i.e our main class. We can play any audio/video or anything in it.
Change your Manifest.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="on.boot.completed"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="on.boot.completed.AfterBootActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="on.boot.completed.Autostart" >
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="on.boot.completed.StarterService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
Also Remember to install it in internal memory because if the app installed on the SD Card then autostart will not work! That’s why it’s important that we add in manifest.
android:installLocation="internalOnly"
That’s all run your app.
After it has been started turn off your phone and turn it back on and the app would start automatically after the device has booted up.

AlarmManager Does not work when Activity in background

In my App, I'm using AlarmManager for transmitting keep-alive every day once a day.
I'm using the following code for starting athe AlaramManager
Calendar calendar = DateUtils
.getNextKeepAliveTime(keepAliveHours, keepAliveMinutes);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),
ALARM_REQUEST_CODE, intent, 0); // PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
Log.d(TAG, "cal: " + calendar.toString());
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
long diff = calendar.getTimeInMillis() - System.currentTimeMillis();
if(diff < 0) diff = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, diff, AlarmManager.INTERVAL_DAY, sender);
When activity goes background, the receiver, AlarmReceiver, does not get any event. But it working successfully when activity in foreground.
Ther receiver declaration in my manifest is -
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
The manifest is
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.yachtdroid.ba.MainActivity"
android:label="#string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.yachtdroid.ba.ui.Settings" >
</activity>
<receiver android:name="MainActivity$ChargingOnReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
<service android:name="com.yachtdroid.ba.services.MailSenderService" />
<service android:name="com.yachtdroid.ba.services.SmsSender" />
<activity android:name="com.yachtdroid.ba.ui.YDSiteView" />
<activity
android:name="com.yachtdroid.ba.ui.BatteryAlertDialog"
android:theme="#style/NoTitleDialog" />
<activity
android:name="com.yachtdroid.ba.ui.ActivityLog"
android:theme="#style/NoTitleDialog" />
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />
<!-- intent-filter>
<action android:name="alarm_action"/>
</intent-filter>
</receiver -->
</application>
The receiver code is
public static class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, intent.getExtras().toString());
if (!inForeground) {
Log.d(TAG, "*** Move app to front!");
Intent it = new Intent("intent.my.action");
it.setComponent(new ComponentName(context.getPackageName(),
MainActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.getApplicationContext().startActivity(it);
}
try {
Toast.makeText(context, "alarm message", Toast.LENGTH_SHORT).show();
MainActivity.instance.sendKeepALive();
} catch (Exception e) {
Toast.makeText(
context,
"There was an error somewhere, but we still received an alarm",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
How it can be solved?
Thanks,
Eyal.
Try to remove the code for the broadcast receiver from the Activity ,make another java file and put it there and also modify your manifest as per that
<receiver android:name=".AlarmReceiver" />
instead of
<receiver android:name="MainActivity$AlarmReceiver" android:enabled="true" />

Categories

Resources