I want to get the id of broadcastriver.
Is there any way to find the id of the broadcastreciver?? I am making an alarm application and on saving the alarm, I send a broadcast using pending intent. Now if a user edit the alarm, I want to abort that specif broadcast and send a new broadcast of the new time?
How can I do this????
I am using this code for broadcast
Intent intent = new Intent(this, StartProfileBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, StartTimemill, pendingIntent);
and in manifest, I register my Broadcastreciver using this code
<receiver android:name="StartProfileBroadcastReceiver" >
</receiver>
you can find the project on my github by this project you can et lots of alarm and can catch cancaled alarms.
Alarm class is needed to hold to get specific data
Alarm.java
public class Alarm {
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public int getUniqueID() {
return uniqueID;
}
public void setUniqueID(int uniqueID) {
this.uniqueID = uniqueID;
}
private int state;
private int uniqueID;
}
AlarmHolder is singleton and holds the alarm in an arraylist
AlarmHolder.java
import java.util.ArrayList;
public class AlarmHolder{
private static AlarmHolder uniqueAlarmHolder;
private ArrayList<Alarm> lAlarms;
private AlarmHolder() {
lAlarms = new ArrayList<Alarm>();
}
public synchronized static AlarmHolder getInstance()
{
if (uniqueAlarmHolder == null) {
uniqueAlarmHolder = new AlarmHolder();
}
return uniqueAlarmHolder;
}
public void registerAlarm(int id) {
Alarm a = new Alarm();
a.setState(1);
a.setUniqueID(id);
lAlarms.add(a);
}
public void removeAlarm(int id,Alarm a) {
Alarm newAlarm = new Alarm();
a.setState(0);
a.setUniqueID(id);
lAlarms.remove(id);
lAlarms.add(newAlarm);
}
public void replaceList(ArrayList<Alarm> newList) {
lAlarms.clear();
lAlarms.addAll(newList);
}
public ArrayList<Alarm> getAlarms() {
return lAlarms;
}
public Alarm lastAlarmId() {
return lAlarms.get(lAlarms.size()-1);
}
}
MyBroadcastReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
AlarmHolder objAlarmHolder = AlarmHolder.getInstance();
#Override
public void onReceive(Context context, Intent intent) {
// with this key we can catch the alarm which we want
int key = intent.getIntExtra("key",0);
for (Alarm alarm : objAlarmHolder.getAlarms()) {
// if alarmstate is 1 and the key belogs that alarm device will vibrate
if (alarm.getState() == 1 & alarm.getUniqueID() == key ) {
Log.v("alarm", String.valueOf(String.valueOf(alarm.getUniqueID())));
Toast.makeText(context, "your alarm id : " + String.valueOf(alarm.getUniqueID()),Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
} else {
Log.v("alarm", "canceled alarm number : " + String.valueOf(alarm.getUniqueID()));
}
}
}
}
MainActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText text;
int uniqueInteger = 1;
AlarmHolder objAlarmHolder = AlarmHolder.getInstance();
ArrayList<Alarm> tempAlarmHolder = new ArrayList<Alarm>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// we should pass unique integer to method
startAlert(uniqueInteger);
uniqueInteger++;
}
});
}
public void startAlert(int rnd) {
int i = Integer.parseInt(text.getText().toString());
Bundle bundle = new Bundle();
bundle.putInt("key", uniqueInteger);
objAlarmHolder.registerAlarm(uniqueInteger);
int alarmCount = 0;
tempAlarmHolder.clear();
// in this loop, all old alarms' state will set as 0
for (Alarm alarm : objAlarmHolder.getAlarms()) {
if(alarmCount < objAlarmHolder.getAlarms().size()-1)
{
Alarm objAlarm = new Alarm();
objAlarm.setState(0);
objAlarm.setUniqueID(alarmCount);
tempAlarmHolder.add(objAlarm);
}
else
{
tempAlarmHolder.add(alarm);
}
alarmCount++;
}
objAlarmHolder.replaceList(tempAlarmHolder);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), getPendingIntent(bundle, uniqueInteger));
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
private PendingIntent getPendingIntent(Bundle bundle, int rc) {
Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
// send alarm id to broatcast
intent.putExtras(bundle);
return PendingIntent.getBroadcast(this, rc, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
Manifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.VIBRATE" ></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver" >
</receiver>
</application>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
You can either:
Keep a reference to the PendingIntent object you register with the AlarmManager, and call PendingIntent.cancel to cancel it when you're registering a new alarm.
Use the flag FLAG_CANCEL_CURRENT when creating your PendingIntent.
Using both methods, the old alarm will be deleted when creating the new one..
Related
I am trying to experiment with services, and I found this example:
Alarm Manager Example.
I tried using the accepted answer, and the changes AndroidDev suggested, but I cant get it to work. Based on the fact that none of my Log messages appear, I think that the Service isn't called.
My onCreate method is this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent serviceIntent = new Intent(this, YourService.class);
startService(serviceIntent);
Vibrator v=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
the Service is this:
package habos.alarmtest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class YourService extends Service
{
Alarm alarm = new Alarm();
String tag="Babis service";
public void onCreate()
{
super.onCreate();
Log.i(tag, "oncreate");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(tag, "onstartcommand");
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;
}
}
The onStart method is deprecated, but onStartCommand should work.
the manifest is this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="habos.alarmtest">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>
<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">
<receiver android:name=".Alarm" android:exported="true">
<intent-filter>
<action android:name="habos.START_ALARM" >
</action>
</intent-filter>
</receiver>
<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>
</application>
and the alarm is this
package habos.alarmtest;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;
import android.os.Vibrator;
public class Alarm extends BroadcastReceiver
{
String tag="Babis alarm";
#Override
public void onReceive(Context context, Intent intent)
{
Log.i(tag, "onreceive");
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, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
Vibrator v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
wl.release();
}
public void setAlarm(Context context)
{
Log.i(tag,"setalarm");
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent("mypackage.START_ALARM");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 30,pi); // Millisec * Second * Minute
}
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);
}
}
added 2 more Logs, before and after the startService,the logcat is here:
https://www.dropbox.com/s/0ksdrfzyjb0qafe/logcat.txt?dl=0
(sorry I didn't paste it directly here, the code block didn't work for some reason)
I followed this tutorial for Repeating Alarm Example in Android using AlarmManager: http://javatechig.com/android/repeat-alarm-example-in-android.
I want to start an alarm which will trigger an intent service every 10 seconds. But it doesn't work, if there is someone who can help me.
MainActivity code:
package subhi.com.broadcast2;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(MainActivity.this, MyService.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 20;
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, pendingIntent);
}
}
The AlarmReciver code, which will start the IntentService:
package subhi.com.broadcast2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by subhi on 2/20/2016.
*/
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
//Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
Intent intent2=new Intent(context,MyService.class);
context.startService(intent);
}
}
The MyService code:
package subhi.com.broadcast2;
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
/**
* Created by subhi on 2/20/2016.
*/
public class MyService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* #param name Used to name the worker thread, important only for debugging.
*/
public MyService() {
super("My_Worker_Thread");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"Service Started....",Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"Service Stopped....",Toast.LENGTH_LONG).show();
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getApplicationContext(),"Good",Toast.LENGTH_LONG).show();
synchronized (this){
int count=0;
while (count<10) {
try {
wait(3000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Finally the manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="subhi.com.broadcast2">
<application
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=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".MyService">
</service>
</application>
</manifest>
The broadcast receiver is having a BOOT_COMPLETED intent filter. Thus the receiver will only receive a broadcast on boot complete. Instead use your MyService intent service in the pending intent as follows
PendingIntent intent = PendingIntent.getService(context, 0, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);
My question is, I want toast message to be displayed when alarm is set to rings for android..I have AnCal alarm application ..I have tried a lot but its not working ...I need your help...
Thanks and regard
Sarfaraz
I am giving you an example that will set alarm every 30 seconds and when the alarm will ring the toast will appear and when alarm will be set notification will be pushed. If I misunderstood your question post comment so i can delete this answer to avoid UN-necessary post.
Step 1 MainActivity.java
package com.example.alarmmanager;
import com.example.alarmmanager.NotificationReceiverActivity;
import com.example.alarmmanager.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private AlarmManagerBroadcastReceiver alarm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarm = new AlarmManagerBroadcastReceiver();
}
#Override
protected void onStart() {
super.onStart();
}
public void startRepeatingTimer(View view) {
Context context = this.getApplicationContext();
if(alarm != null){
alarm.SetAlarm(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
public void cancelRepeatingTimer(View view){
Context context = this.getApplicationContext();
if(alarm != null){
alarm.CancelAlarm(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
public void onetimeTimer(View view){
Context context = this.getApplicationContext();
if(alarm != null){
alarm.setOnetimeTimer(context);
createNotification(view);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
public void createNotification(View view) {
/*********** Create notification ***********/
final NotificationManager mgr=
(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.ic_launcher,
"Android Example Status message!",
System.currentTimeMillis());
// This pending intent will open after notification click
PendingIntent i= PendingIntent.getActivity(this, 0,
new Intent(this, NotificationReceiverActivity.class),
0);
note.setLatestEventInfo(this, "Android Example Notification Title",
"This is the android example notification message", i);
//After uncomment this line you will see number of notification arrived
//note.number=2;
mgr.notify(0, note);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step 2 AlarmManagerBroadcastReceiver.java
package com.example.alarmmanager;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.Toast;
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver{
final public static String ONE_TIME = "onetime";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
//You can do the processing here update the widget/remote views.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));
Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
//Release the lock
wl.release();
Intent service1 = new Intent(context, NotificationService.class);
context.startService(service1);
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 30 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi);
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
public void setOnetimeTimer(Context context){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}
Step 3 NotificationReceiverActivity.java
package com.example.alarmmanager;
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiverActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
}
Step 4 NotificationService.java // this is to push notification you can remove this if you do not want notifications
package com.example.alarmmanager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
public class NotificationService extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId)
{
final NotificationManager mgr=
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note=new Notification(R.drawable.ic_launcher,
"Android Example Status message!",
System.currentTimeMillis());
// This pending intent will open after notification click
PendingIntent i= PendingIntent.getActivity(this, 0,
new Intent(this, NotificationReceiverActivity.class),
0);
note.setLatestEventInfo(this, "Android Example Notification Title",
"This is the android example notification message", i);
//After uncomment this line you will see number of notification arrived
note.number=startId;
mgr.notify(0, note);
}
#Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
Step 5 : AndroidManifest.xml
<uses-permission android:name='android.permission.WAKE_LOCK'/>
<receiver android:name="com.example.alarmmanager.AlarmManagerBroadcastReceiver"> </receiver>
<service android:name="com.example.alarmmanager.NotificationService"
android:enabled="true" />
Step 6 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:onClick="startRepeatingTimer"
android:text="Start Alarm repeating 30 sec" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginTop="50dp"
android:layout_toRightOf="#+id/textView1"
android:onClick="cancelRepeatingTimer"
android:text="Cancel Alarm" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp"
android:onClick="onetimeTimer"
android:text="Start One Time Alarm" />
</RelativeLayout>
Step 7 result.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:text="This is the result activity opened from the notification"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/textView1"></TextView>
</RelativeLayout>
Step 8 Run project
I have an Activity class, in which I have a static flag, let's say
public static volatile flag = false;
Then in the class, I start a thread, which checks the flag and do different things.
I also have a broadcastreceiver, which sets the flag to true or false.
I though volatile will force the flag to the most recent value. But I can see my broadcastreceiver sets the static flag to true, but my thread is still getting it as false.
Am I missing something basic here? Any help would be appreciated!
Simplified Code (Updated) - So the flag is supposed to change to true after one minute. But it never did. But message from broadcast receiver shows it has been change to true
TestappActivity.java:
package com.test;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class TestappActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent0 = new Intent(this, TestService.class);
this.startService(intent0);
Intent intent = new Intent(this, TestReceiver.class);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent sender = PendingIntent.getBroadcast(this,
1, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Calendar slot = Calendar.getInstance();
int min = slot.get(Calendar.MINUTE);
slot.set(Calendar.MINUTE, min+1);
am.set(AlarmManager.RTC_WAKEUP, slot.getTimeInMillis(), sender);
}
}
TestService.java:
package com.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class TestService extends Service {
private static final String TAG = "TestService";
public static volatile boolean flag = false;
private MyTopThread mTopThread;
public TestService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
}
#Override
public void onDestroy() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
protect();
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
/**
* Run protection
*
*/
private void protect() {
mTopThread = new MyTopThread();
mTopThread.start();
}
private class MyTopThread extends Thread {
#Override
public void run() {
while (true) {
try {
Thread.sleep(150);
Log.d(TAG, "Flag is " + TestService.flag);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
TestReceiver.java:
package com.test;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class TestReceiver extends BroadcastReceiver {
final static private String TAG = "TestReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive is triggered ...");
TestService.flag = true;
Log.d(TAG, "flag is changed to " + TestService.flag);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestappActivity"
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=".TestService" />
<receiver
android:name=".TestReceiver"
android:process=":remote" >
</receiver>
</application>
</manifest>
I think the problem is that you are running the receiver in its own process. From the docs for the android:process attribute of <receiver>:
If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the broadcast receiver runs in that process.
I think the receiver is modifying a process-local version of TestService.flag, not the one being used by TestService. Try removing the android:process attribute from the <receiver> tag in your manifest.
From this link
http://www.javamex.com/tutorials/synchronization_volatile.shtml
Essentially, volatile is used to indicate that a variable's value will
be modified by different threads.
I really hope your service thread is not this one (I don't see any other one):
private class MyTopThread extends Thread {
#Override
public void run() {
while (true) {
try {
Thread.sleep(150);
Log.d(TAG, "Flag is " + TestService.flag);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Because you have while(true) here, not while(!flag) as it should be.
I am making an alarm application. It's working with some hardcoded values like
calendar.add(Calendar.SECOND,30);
after 30 second alarm is firing but it's not working with
cal.set(Calendar.AM_PM,Calendar.PM);
cal.set(Calendar.MONTH,8);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.DAY_OF_MONTH,4);
cal.set(Calendar.HOUR_OF_DAY,5);
cal.set(Calendar.MINUTE,30);
Here is my whole code:
........Activity class..................
public class SampleAlarm1Activity extends Activity {
public Context context;
private boolean alarmstarted=true;
Button setalarm;
Button start;
Button cancel;
Edit Text year;
EditText month;
EditText date;
EditText hour;
EditText minute;
Intent i;
PendingIntent pi;
AlarmManager am;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=getApplicationContext();
setalarm=(Button)findViewById(R.id.setalarm);
start=(Button) findViewById(R.id.start);
cancel=(Button)findViewById(R.id.cancelalarm);
month=(EditText)findViewById(R.id.month);
date=(EditText)findViewById(R.id.date);
hour=(EditText)findViewById(R.id.hour);
minute=(EditText)findViewById(R.id.minute);
setalarm.setOnClickListener(setalarmListener);
start.setOnClickListener(startalarmListener);
cancel.setOnClickListener(cancelalarm);
start.setVisibility(android.view.View.INVISIBLE);
month.setVisibility(android.view.View.INVISIBLE);
date.setVisibility(android.view.View.INVISIBLE);
hour.setVisibility(android.view.View.INVISIBLE);
minute.setVisibility(android.view.View.INVISIBLE);
}
private OnClickListener setalarmListener=new OnClickListener() {
#Override
public void onClick(View v) {
month.setVisibility(android.view.View.VISIBLE);
date.setVisibility(android.view.View.VISIBLE);
hour.setVisibility(android.view.View.VISIBLE);
minute.setVisibility(android.view.View.VISIBLE);
start.setVisibility(android.view.View.VISIBLE);
}
};
private OnClickListener startalarmListener=new OnClickListener() {
#Override
public void onClick(View v) {
int mon = 0,dat = 0,hr = 0,min = 0;
try{
String mon2=month.getText().toString();
System.out.println("month string entered::::::::::"+mon2);
mon=Integer.parseInt(mon2);
System.out.println("month int valuereturn entered::::::::::"+mon);
String dat2=date.getText().toString();
dat=Integer.parseInt(dat2);
System.out.println("date entered::::::::::"+dat);
String hr2=hour.getText().toString();
hr=Integer.parseInt(hr2);
System.out.println("hour entered::::::::::"+hr);
String min2=minute.getText().toString();
min=Integer.parseInt(min2);
System.out.println("minute entered::::::::::"+min);
}
catch(NumberFormatException exc){
System.out.println("exceptin is :"+exc);
}
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.AM_PM,Calendar.PM);
cal.set(Calendar.MONTH,mon);
System.out.println("month string entered::::::::::"+mon);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.DAY_OF_MONTH,dat);
System.out.println("date string entered::::::::::"+dat);
cal.set(Calendar.HOUR_OF_DAY,hr);
System.out.println("hour entered::::::::::"+hr);
cal.set(Calendar.MINUTE,min);
System.out.println("minute entered::::::::::"+min);
i = new Intent(SampleAlarm1Activity.this, AlarmReceiver.class);
pi=PendingIntent.getBroadcast(SampleAlarm1Activity.this, 0, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pi);
System.out.println("alarm on in activity ");
Toast.makeText(context, "alarm on activity", Toast.LENGTH_SHORT).show();
}
};
private OnClickListener cancelalarm=new OnClickListener() {
#Override
public void onClick(View v) {
if(alarmstarted)
try{
am.cancel(pi);
}catch(Exception e){
System.out.println("alarm not started"+e);
}
System.out.println("alarm cancel ");
Toast.makeText(context, "alarm cancel", Toast.LENGTH_SHORT).show();
}
};
}
broadcast receiver code....in same package ...........................................
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
System.out.println("alarm start");
Toast.makeText(context, "alarm is working",Toast.LENGTH_SHORT).show();
}
}
Manifest file..................................
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.SampleAlarm"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SET_ALARM"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"></uses-permission>
<uses-permission android:name="android.permission.SET_TIME"></uses-permission>
<uses-permission android:name="android.permission.SET_TIME_ZONE"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SampleAlarm1Activity"
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=".AlarmReceiver"></receiver>
</application>
</manifest>
I had make the same application but I had select the time from Time Picker but you can put your own time. Here I post my sample application classes. It may be help you..
SampleReminderActivity.java
package com.reminder;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SampleReminderActivity extends Activity {
private Button btn = null;
private AlarmManager alarmManager = null;
Calendar cal = Calendar.getInstance();
private Calendar calDay = Calendar.getInstance();
private Calendar dateStart = Calendar.getInstance();
static final int DIALOG_TIME = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
btn = (Button) findViewById(R.id.btnClick);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
showDialog(DIALOG_TIME);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id){
case DIALOG_TIME:
dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
// c.clear();
c.set(Calendar.YEAR, hourOfDay);
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
Log.d("SampleRemainder", "Before Intent");
Intent intent = new Intent(SampleRemainderActivity.this,AlarmReceiver.class);
// startActivity(intent);
Log.d("SampleRemainder", "After Intent");
Log.d("SampleRemainder", "Before PendingIntent");
PendingIntent pi = PendingIntent.getBroadcast(SampleRemainderActivity.this, 0, intent, 0);
Log.d("SampleRemainder", "After PendingIntent");
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
Toast.makeText(SampleRemainderActivity.this, "Alarm has been set..", Toast.LENGTH_LONG).show();
}
},cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),false);
Log.d("SampleRemainder", "get cal Hour **" + cal.get(Calendar.HOUR_OF_DAY));
Log.d("SampleRemainder", "get cal Minute **" + cal.get(Calendar.MINUTE));
break;
}
return dialog;
}
}
AlarmReceiver.java
package com.reminder;
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)
{
Log.d("AlarmReceiver","in AlarmReceiver"+ context.toString());
Intent i = new Intent(context, AlarmActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
AlarmActivity.java
package com.reminder;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
public class AlarmActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
Log.d("AlarmActivity","in AlarmActivity");
super.onCreate(savedInstanceState);
new AlertDialog.Builder(AlarmActivity.this).setTitle("Task").setMessage("Time to wake up").setPositiveButton("ok", new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
AlarmActivity.this.finish();
}
}).create().show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button
android:id="#+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
/>
</LinearLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reminder"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SampleReminderActivity"
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=".AlarmActivity"></activity>
<receiver android:name="com.reminder.AlarmReceiver" android:process=":remote"></receiver>
</application>
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Hi friend my problem is solved.
Note: use
cal.set(Calendar.AM_PM,Calendar.AM or PM);
and cal.set(Calendar.HOUR,hour); for 12 hour format.
otherwise use only cal.set(Calendar.HOUR_OF_DAY, hour) for 24 hour format.
Other important thing i was doing mistake.
cal.set(Calendar.MONTH, month);
I was using 8 for august but it should be 7.
Actualy For January constant value is 0.
Thanks for your attention.