I HAVE to be missing something, I made a test Application Project for myself and this works perfectly there but when I tried to implement my AlarmManager into my main project's fragment it just won't work. Here's my code:
The Method that is in my fragment:
public void schedule()
{
Long time = new GregorianCalendar().getTimeInMillis()+10*1000;
Intent intent = new Intent(getActivity(), AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, time, 10*1000, PendingIntent.getBroadcast(getActivity(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
}
and here is my AlarmReceiver.class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
}
}
And also here are the preparations in my Manifest file:
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
... >
<receiver android:name=".AlarmReceiver"/>
What am I doing wrong?
Note: It seems that it doesn't even reach the AlarmReceiver
Your code seems to work.
But it the problem is the manifest. the
<receiver android:name=".AlarmReceiver"/>
should be the with the full package name, for example
<receiver android:name="com.example.AlarmReceiver"/>
I've double check it with my app. so you are good to go.
You are using the receiver incorrectly. What you need to do first is to add an action to your receiver's manifest
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.example.action.ALARM" />
</intent-filter>
Then, construct the intent with that action:
public void schedule() {
Long time = new GregorianCalendar().getTimeInMillis()+10*1000;
Intent intent = new Intent("com.example.action.ALARM");
...
}
and in your receiver:
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.example.action.ALARM")
Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
}
This should do the trick.
Related
I'm attempting to make it so my app runs some code once per day at 6AM. This works just fine when the app is open and in the foreground, but if the app is closed by swiping it away, the code is never called at the appropriate time.
AlarmReceiver.java (For testing purposes, I have it just trying to display a Toast to verify it runs)
public class AlarmReceiver extends BroadcastReceiver {
public static final String intentAction = "com.mpagliaro98.action.NOTIFICATIONS";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(intentAction)) {
Toast.makeText(context, "RECEIVER CALLED", Toast.LENGTH_LONG).show();
}
}
}
MainActivity.java (Where the alarm is being set)
public class MainActivity extends AppCompatActivity {
...
private void setRecurringAlarm() {
// Set this to run at 6am
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 6);
updateTime.set(Calendar.MINUTE, 0);
updateTime.set(Calendar.SECOND, 0);
updateTime.set(Calendar.MILLISECOND, 0);
// Build the pending intent and set the alarm
Intent i = new Intent(AlarmReceiver.intentAction);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(),
0, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
assert am != null;
am.setRepeating(AlarmManager.RTC, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}
}
AndroidManifest.xml (Just the relevant parts)
<uses-permission android:name="android.permission.SET_ALARM" />
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.mpagliaro98.action.NOTIFICATIONS" />
</intent-filter>
</receiver>
I've read through dozens of similar problems to this on this site and elsewhere and I'm seriously at a loss for why this won't work. Any help would be appreciated.
Try changing receiver to
<receiver android:process=":remote" android:name="AlarmReceiver"></receiver>
Should I use android: process =":remote" in my receiver?
here is my manifest
<receiver android:name=".MyCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and
public class MyCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
some code
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
some code
}
}
}
it is works but after sometime that press back button and phone be idle it doesn't work any more
(i added "android.os.Process.killProcess(android.os.Process.myPid());" at the end of my code and now it is better and work for maybe 2 3 hour after last execute)
You can use alarm manger to broadcast receiver after certain interval of time like this.
public static void scheduleTestAlarmReceiver(Context context) {
Intent receiverIntent = new Intent(context, TestAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+startDelay, someDelay, sender);
}
In my code alarm manger is not working.Rest of my application is working well.Please see my code.
Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.putExtra("class", "home");
PendingIntent pendingIntent = PendingIntent.getService(this, 0,myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);
and my android AlarmService class:-
public class AndroidAlarmService extends BroadcastReceiver implements URLs{
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("BroadCast\n");
String name=intent.getStringExtra("class");
if(name.equals("home")){
Intent homeIn=new Intent(context,Home.class);
context.startActivity(homeIn);
}
}
}
in manifest I have done this;
<receiver android:name=".AndroidAlarmService" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
Why its not working??
I got the answer.I made following changes:
Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);
In my AndroidAlarmService class:
Intent homeIn=new Intent(context,Home.class);
homeIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIn);
I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.
Simply changed:
<receiver android:name=".AndroidAlarmService" android:enabled="true" >
for:
<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
Have you tried changing the
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);
Have you tried changing the 6000 to something else? It seems like you have everything else correct.
EDIT:
Make sure you have the Read_phone_state permission in your manifest.
Code of Receiver:
public class OnBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
BufferedReader in=new BufferedReader(
new FileReader(MainNote.log));
String AlarmString;
int i = 0;
while ((AlarmString = in.readLine())!=null)
{
Long AlarmTime = Long.parseLong(AlarmString.substring(0, AlarmString.indexOf(" ")));
if (AlarmTime < System.currentTimeMillis()) i++;
else {
String AlarmArray[] = AlarmString.split("\\s");
Intent AlarmIntent = new Intent(context, AlarmReceiver.class);
if (AlarmArray[1].equals("null")) AlarmIntent.putExtra("alarm_message", "");
else AlarmIntent.putExtra("alarm_message", AlarmArray[1]);
if (AlarmArray[2].equals("true"))
AlarmIntent.putExtra("Vibration", true);
else AlarmIntent.putExtra("Vibration", false);
if (AlarmArray[3].equals("true"))
AlarmIntent.putExtra("Sound", true);
else AlarmIntent.putExtra("Sound", false);
final int _ID = (int) System.currentTimeMillis();
PendingIntent sender = PendingIntent.getBroadcast(context , _ID, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, AlarmTime, sender);
}
}
Code of AlarmReceiver.class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String message = bundle.getString("alarm_message");
boolean Vibration = bundle.getBoolean("Vibration");
boolean Sound = bundle.getBoolean("Sound");
if (message.equals(null))
NotifierHelper.sendNotification(context, MainNote.class, context.getResources().getString(R.string.NotTitle), context.getResources().getString(R.string.Nodiscr), 1, Sound, true, Vibration);
else
NotifierHelper.sendNotification(context, MainNote.class, context.getResources().getString(R.string.NotTitle), message, 1, Sound, true, Vibration);
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:label="#string/app_name" android:name=".MainNote">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FingerPaint"></activity>
<receiver android:process=":remote" android:name=".AlarmReceiver"></receiver>
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
MainNote.log can costist for example
1304094118826 text true true
After reboot I see that my process is started, but then I dont have Notifacation. Whats wrong here? And how to debug code in OnBootReciever?
I replace my code in OnBootReciever on
Intent AlarmIntent = new Intent(context, AlarmReceiver.class);
AlarmIntent.putExtra("alarm_message", "blabla");
AlarmIntent.putExtra("Vibration", true);
AlarmIntent.putExtra("Sound", true);
final int _ID = (int) System.currentTimeMillis();
PendingIntent sender = PendingIntent.getBroadcast(context , _ID, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+6000, sender);
And its work. So problem in part where I read information from file.
One problem was in MainNote.log. Log its static varible so I cant contact with this here. But now I have very strange problem -
java.io.FileNotFoundException: /mnt/sdcard/AlarmsFromDrawNote.alm (Permission denied)
com.notedraw.bos.app.OnBootReceiver1.onReceive(OnBootReceiver1.java:30)
30th line is -
BufferedReader in=new BufferedReader(new FileReader(log));
WTF? (see manifest)
Your AlarmReceiver needs to be a static BroadcastReceiver like your OnBootReceiver is in order for it to act on the alarm that you set. Unfortunately, I don't think you can have more than one static receiver per application (I tried before and it did not work for me). You could try putting two intent filters on your OnBootReceiver and have it deal with both broadcasts.
In your onRecieve, simply check intent.getAction() to see which broadcast it is receiving (the boot up one or the alarm), and deal with it appropriately.
You need to create a new broadcast receiver inside the class where the broadcast will be received. Put this at the top of your class outside any methods.
AlarmReceiver intentReceiver = new AlarmReceiver ();
And you need:
#Override
protected void onPause() {
// TODO Mark time user logged out
unregisterReceiver(intentReceiver);
super.onPause();
}`
And:
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter(CustomAction.MY_INTENT);
filter.addAction(CustomAction.MY_INTENT);
registerReceiver(intentReceiver, filter);
super.onResume();
}
I"m sure this is something that is simple, but I'm not figuring it out. I'm trying to make a simple repeating alarm and it never gets triggered. What I have is:
private void setupAlarms()
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, RepeatingAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid.this, 0, intent, 0);
GregorianCalendar fifteenSeconds = (GregorianCalendar)Calendar.getInstance();
fifteenSeconds.add(Calendar.MINUTE, 0);
fifteenSeconds.set(Calendar.SECOND, 15);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), fifteenSeconds.getTimeInMillis(), pendingIntent);
}
This is called from the main onCreate call.
My alarm receiver:
public class RepeatingAlarm extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, R.string.hello, Toast.LENGTH_SHORT).show();
}
}
In my manifest, I have added:
<receiver android:name=".RepeatingAlarm" android:process=":remote" />
Any help, much appreciated
Have you added an intent filter to your BroadcastReceiver?
Code might look something like this in your AndroidManifest.xml file:
<receiver android:name=".RepeatingAlarm" android:exported="true">
<intent-filter>
<action android:name="intent id text" />
</intent-filter>
</receiver>
and when creating your intent do something like this:
Intent intent = new Intent("intent id text")
where the "intent id text" can be any string you use to identify your intent. Also Android alarms get reset if you reboot your device so that may be something you need to look into.
Hope this helps.