Scheduling alarm every 2 minutes android - android

In my activity class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,System.currentTimeMillis(),2000, pendingIntent);
}
And My onrecieve function in alarmreciever class
#Override
public void onReceive(Context context, Intent intent)
{
//get and send location information
System.out.println("fired");
}
I am using nexus 4, kitkat version. I don't see any onreceive function fired every 2 minutes.nthg is happening...
any help? thank you
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<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:name="com.example.AlarmExample"
android:exported="false" >
</receiver>
</application>
</manifest>
I just put my manifest as well. ................................................

In your setRepeating function, you should use SystemClock.elapsedRealTime() for ELAPSED_REALTIME_WAKEUP.
Also, you need to change 2000 to 2*60*1000 to specify your interval time.
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
2*60*1000,
pendingIntent);
Hope this helps.
Reference: ELAPSED_REALTIME_WAKEUP
EDIT:
In your manifest file, there is a typo in your receiver name.
Change ".AlarmReciever" to ".AlarmReceiver".
<receiver
android:name=".AlarmReceiver"
android:exported="true" >
</receiver>

in your code you set the alarm this way
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
System.currentTimeMillis(),
2000,
pendingIntent);
the interval time is wrong to run every two minutes you should write:
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
0,
1000 * 60 * 2,
pendingIntent);
EDIT
for your pending intent set flag PendingIntent.FLAG_UPDATE_CURRENT and see if it changes anything.
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);

Related

BroadcastReceiver not being called by Alarm(Manager)

Can you spot any reason as to why BroadcastReceiver is not being called when the Alarm goes off? If I have the alarm launch an explicit intent, it works just fine and my activity opens. If I set the intent to open my BroadCastReceiver then nothing happens so I think there may be something wrong with my receiver class or the Manifest. Here's how I setup the alarm:
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 324, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
mAlarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Here's my broadcast receiver:
public class AlarmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("MJH", "Alarm called...");
Toast.makeText(context, "Alarm...", Toast.LENGTH_LONG).show();
}
}
And here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="mjh.com.apod"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Required to act as a custom watch face. -->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
<receiver
android:name=".AlarmBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:process=":remote">
</receiver>
</application>
Thank you so very much for your time.
I think you should be using PendingIntent.getBroadcast( instead of PendingIntent.getActivity(.
I had to call getBroadcast rather than getActivity to create the PendingIntent. That fixed it.

Notification manager not working with alarmManager

I am trying to use alarm manager to show notification after 5 sec. I have tried many sites but was not able to understand please give a simple example for explaining how to use alarm manager and connect notification with it.
I am newbie.
this is function i used to set alarm and I am not getting notification after 5 secs actually not at all not in emulator nor in android mobile. If put code to create notififcation with a button pressing that is working great.
public void setAlarm(View view)
{
Intent alertIntent = new Intent(this, AlertReciver.class);
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
tv.setText("completed");
}
and this class to make it work
public class AlertReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"times Up", "5 SEcond has passed", "Alert");
}
public void createNotification(Context context,String msg, String msgText, String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ne)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
this is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deepaksingh.goinnotifying" >
<uses-permission android:name="android.permission.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</application>
You havn't set permission for wake:
<uses-permission android:name="android.permission.WAKE_LOCK" />
ANd you didn't register your recevier.Here.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<receiver android:name=".AlertReciver "/>
</application>

Alarm receive don't work

I'm making an alarm receiver, but the alarm is not triggered.
This is my code :
Start alarm in MainActivity :
private void setupAlarm(){
Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);
Log.e("setupAlarm", "Setup alarm complete");
}
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonelogger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="SettingActivity" android:label="#string/action_settings" ></activity>
<service android:name="com.logdata.LogManager" />
<receiver
android:name="com.logdata.AlarmReceiver"
android:enabled="true">
</receiver>
</application>
AlarmReceiver :
package com.logdata;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, com.logdata.LogManager.class);
context.startService(myIntent);
Log.e("AlarmReceiver", "Get message");
}
}
Can someone tell me what the problem is ?
Your Intent is used to explicitly start the AlarmReceiver class which is a BroadcastReceiver. Hence, you need to use getBroadcast(), not getService(), to create the PendingIntent object.
Replace
PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);
with
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Try this. This will work.
Try To Use THis. It will Work for you
private void setupAlarm(){
Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);
Log.e("setupAlarm", "Setup alarm complete");
}

Launching an intent from BroadcastReceiver?

Hi this is my first question on stackoverflow and I'm a beginner on Android programming. I've looked through numerous webpages but can't seem to find my solution. What I'm trying to do is launch a new activity when a button is pressed after 5 seconds with a BroadcastReceiver. This activity will have a new UI with graphics and sounds (eventually). I got the BroadcastReceiver to work but the program crashes when it tries to launch the new intent. What am I doing wrong?
The method in my MainActivity class:
public void setAlarm(){
Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 5 * 1000, pendingIntent);
}
My BroadcastReceiver class:
public class MyBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
Intent intent1 = new Intent(context, ShakeActivity.class);
context.startActivity(intent1);
Toast.makeText(context, "Broadcast works", Toast.LENGTH_LONG).show();
}
}
Manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shakecounter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.shakecounter.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.shakecounter.ShakeActivity"
android:label="#string/title_activity_shake" >
</activity>
<receiver android:name="com.example.shakecounter.MyBroadcastReceiver">
</receiver>
</application>
</manifest>
Intent intent1 = new Intent(context, ShakeActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
Try this one.
Intent iAlarm = new Intent( mcontext1, Alertdialogclass.class );
iAlarm.addFlags(Intent.FLAG_FROM_BACKGROUND);
iAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext1.startActivity(iAlarm);
You have to put the flag FLAG_ACTIVITY_NEW_TASK when You are starting from inside a Receiver:
Intent intent1 = new Intent(context, ShakeActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
If there is another problem, You should post the logcat.

Android: Nothing happens when setting alarm with AlarmManager?

So i'm setting a repeating alarm to fetch data from a server. The problem is that nothing happens when the alarm is suppose to go off. I get no errors while launching it, and the code is correct (as far as i know).
Here is the code that sets the alarm:
private void startRequestTimer() {
// This is what will be called when the alarm goes off (GetOperations)
Intent intent = new Intent(MainActivity.this, GetOperations.class);
PendingIntent operation = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
// The alarm will go off in one second from now
long firstTime = SystemClock.elapsedRealtime();
firstTime += 1000;
// Set the alarm
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 5*1000, operation);
}
Here is GetOperations.java (it's called when the alarm goes off)
package se.jbhalmstad.ndroid;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class GetOperations extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Oh! The alarm went off!", 2000).show();
}
}
And the manifest, in case i messed something up here (which i doubt, since i'm not getting any errors):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.jbhalmstad.ndroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/APP_NAME">
<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>
<activity android:name=".SettingsActivity" />
<activity android:name=".GetOperations" />
</application>
</manifest>
Have i missed something?
You declared GetOperations as a BroadcastReceiver, but in your Manifest it's an Activity.

Categories

Resources