In my project, I'm using alarm code. There is no error, but anything within the BroadcastReceiver class is not being executed. I don't understand where the fault is.
Im doing it in Windows using Eclipse. I also have specified the <receiver> class in AndroidManifest.xml.
I want the code whithin the BroadcastReceiver class to be executed. In this case, I want the text given whithin the receiver class to be displayed at the specified time.
This is my receiver class:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.registerReceiver(null, null);
Toast.makeText(context, "Time is
up!!!!.",Toast.LENGTH_LONG).show();
}}
Can anyone suggest a way to get rid of this problem?
Thanks!!!
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.rappel"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<provider
android:name="ScheduleProvider"
android:authorities="com.project.rappel" />
<activity
android:name=".Rappel"
android:label="#string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SetSchedule"></activity>
<activity
android:name=".DaysAndTimes"></activity>
<activity
android:name=".Tts"></activity>
<receiver
android:name="MyBroadcastReceiver"
android:process=":remote" />
</application>
<uses-sdk
android:minSdkVersion="8" />
</manifest>
Above is my androidmanifest.xml.This is the code I used for trigerring the Receiver.
public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
Looking only at this piece code it is not easy to answer.
However, it is likely that you have not specified the intent filter for the receiver in the manifest.
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="xyz.abc.YOUR_ACTION" />
</intent-filter>
</receiver>
The activity or service that is using the alarm, when "the time is up" has to send an Intent matching the action specified in the android manifest. You have to do something like this:
Intent MyIntent = new Intent("xyz.abc.YOUR_ACTION");
Context cont = this.getBaseContext();
cont.sendBroadcast(SMSIntent);
Hope this helps.
Cheers
I think the problem is in manifest file.
Try to remove this parameter "android:process=":remote"" from your reciever.
EDIT:
Please, see my code bellow it work's perfectly. Hope, it will help.
// registration
Intent intent = new Intent(context, AlarmReceiver.class).putExtra(AlarmReceiver.ALARM_ID, alarm.getId());
intent.setData(Uri.parse("date:" + alarm.getId()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // try to change last parameter like mine
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTime().getTime(), pendingIntent);
// in manifest in application tag
<receiver android:name=".ui.more.AlarmReceiver"/>
// receiver class
public class AlarmReceiver extends BroadcastReceiver {
public static final String ALARM_ID = "alarmId";
public void onReceive(Context context, Intent intent) {
context.startActivity(new Intent(context, StartAlarmActivity.class)
.putExtra(StartAlarmActivity.ALARM_ID, intent.getExtras().getInt(ALARM_ID))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
First try adding a period in your manifest file in front of MyBroadcastReceiver. It should look like: android:name=".MyBroadcastReceiver".
If that doesn't work (though hopefully it does) try adding 'Context.' before ALARM_SERVICE on this line:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
so it would look like:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Otherwise, I am not seeing anything immediately obvious.
This code works fine:
public void setupAlarm() {
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
// Set Alarm for next 10 seconds
time.add(Calendar.SECOND, 10);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
pendingIntent);
}
and in the androidmanifest.xml I used this code:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".MyAlarmReceiver" />
<activity
android:name=".AlarmSampleActivity"
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>
Thanks all for helping!!!
Related
I have two issues of Alarm manager. One is i want to repeat alarm every 10 seconds and other is my Broadcast Receiver is invoking after 10 second.
When i use Activity instead of Broadcast Receiver it is working But Broadcast Receiver is not working. And time is not proper some time it works in 1 min and some times it works i 50 Seconds. But i need every 10 Second.
My code is:
buttonStart.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
long tenMin = 1000/5 ;//here is 30 seconds
Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, intent, 0);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), tenMin, pendingIntent);
}});
BoradcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
Manifest file:
<application
android:allowBackup="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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyAlarmService" />
<receiver
android:name=".MyBroadcastReceiver"
android:process=":remote"></receiver>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Use:
Activity:
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);
10sec repeating interval,
repeatingInterval = 10 *1000; //interval in milli seconds for 10sec
AlarmManager alarmManager =(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
alarmManager .setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), repeatingInterval , pendingIntent);
It should work.
I'm having a requirement where i need to open an activity at specific time and perform specific task even when the app is killed. That is even when the app is removed from multi-task window pane.
As of now i'm using the alarm manager to achieve this task as shown below.
Intent myIntent = new Intent(getBaseContext(),MyScheduledReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),
0, myIntent, 0);
AlarmManager alarmManager
= (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
long interval = 60 * 1000; //
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), interval, pendingIntent);
finish();
The onReceive method is as below:
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
The problem is, it opens up the activity when the app is in background. Not when in closed or not in the back stack(mutli-task pane).
Please lighten me up. I'm struggling.
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidScheduledActivity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AndroidScheduledActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyScheduledActivity" />
<receiver android:process=":remote"
android:name="MyScheduledReceiver" />
</application>
</manifest>
start a service and your alarm code in that.Once the alarm is done start your application through notification or as per your logic.
You should use Service, Open your activity through service. Service
Hi I am creating an application which will fire the alarm in a particular interval regularly. It works well. But when i switched off my mobile and switched on again alarm not working. Please help to solve the issue.
My Alarm code is:
AlarmManager alarmMgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
alarmId, intent, 0);
Calendar calendar = Calendar.getInstance();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
Add this in your AndroidManifest.xml:
<receiver android:name=".BootCompletedIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Add this class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}
}
Create a BroadCastReceiver and call this put this alarm code in this receiver and add Boot Complete permission to that receiver so when your phone is switched on then that receiver will automatically called. Refer this :
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
AlarmManager alarmMgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
alarmId, intent, 0);
Calendar calendar = Calendar.getInstance();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
}
}
AndroidManifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package_name" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
I am trying to setup an alarm at a specified time, but it is not being caught in my reciver.
Setup:
Intent intent = new Intent(this, ActionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar current = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (current.getTimeInMillis() + 60000),3600000, pendingIntent);
Here is my reciver:
public class ActionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras(); //breakpoint here that doesn't get triggered
}
}
I have put these values in my manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name="com.project.ActionReceiver" android:enabled="true" />
Not sure what is wrong... thanks!
Finally got the receiver to fire! I added the following code to my manifest:
<receiver
android:name="com.project.ActionReceiver"
android:exported="true" >
<intent-filter>
<action android:name="com.project.ActionSetter" >
</action>
</intent-filter>
</receiver>
Found here with details: https://stackoverflow.com/a/16119351/1174574
The name of receiver in your manifest should be class name, such as:
<receiver android:name="com.project.ActionReceiver">
BTW, set an action is a better practice.
Intent intent = new Intent(this, ActionReceiver.class);
intent.setAction("com.project.action.ALERM");
And in the manifest
<receiver android:name="com.project.ActionReceiver">
<intent-filter>
<action android:name="com.project.action.ALERM"/>
</intent-filter>
</receiver>
Try changing the android:name attribute of your receiver to the fully qualified class name of your ActionReceiver. Something like:
<receiver android:name="com.project.ActionReceiver" android:enabled="true" />
I am doing an alarm application, I take code of examples I have found in Internet but it doesn't work I don't know why.
Here is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="iiriondo.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".LoginActivity"
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:name=".OnAlarmReceiver" ></receiver>
</application>
</manifest>
Here the class that listen to the alarm:
public class OnAlarmReceiver extends BroadcastReceiver{
private static int NOTIFICATION_ID = 1;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "La Alarma está sonando",Toast.LENGTH_LONG).show();
}
}
And finally I use this code for set the Alarm:
Intent intent = new Intent(getApplicationContext(),OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 1);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ (5 * 1000), pendingIntent);
Your code is perfectly fine.
Only thing you make sure your java files are located in same package package="iiriondo.activity"
Write Below Intent Code instead of your intent code.
Intent intent = new Intent(MainActivity.this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + (5 * 1000),pendingIntent);
can try with passing the to passing "this" instaed of the getApplicationContext()
new Intent(this,OnAlarmReceiver.class);