Creating a repeating notification with a receiver ( BroadcastReceiver ) - android

I have created a receiver that Extends BroadcastReceiver. This is used to execute my Notification that repeats daily, based on the time selected by the user. For some reason i cant get this to work. I am worried that i am getting the time from the number pickers wrong, but i am new to programing and could use some help. Thank you in advance. Let me know if you see any Errors.
Here is my main activity (MyActivity)
public class MyActivity extends Activity {
TimePicker timePicker;
Button setAlarm;
private int hour;
private int minute;
PendingIntent pendingIntent;
int AM_PM;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
timePicker = (TimePicker) findViewById(R.id.timePicker);
setAlarm = (Button) findViewById(R.id.setUpAlarm);
setAlarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setAlarm();
}
});
}
private void setAlarm() {
int hour = timePicker.getCurrentHour();
int minute = timePicker.getCurrentMinute();
long time = 60 * hour + minute;
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, NotifyService.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
//create alarms
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
// AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
Here is my Notification class ( extends BroadcatsReceiver )
public class NotifyService extends BroadcastReceiver {
public NotifyService() {
}
#Override
public void onReceive(Context context, Intent intent) {
//generate notification // should be splash activity
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(null)
.setContentText("Your tip for today is ready")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
}
// dont forget to compile "com.android.support:support-v4:18.0.+"
}
Here is my manifest
<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=".MyActivity"
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=".NotifyService"
android:enabled="true"
android:exported="true" >
</receiver>
</application>

Dont use a receiver with a filter in your android manifest. Create an 'explicit intent' instead of using an intent filter. This way,
Intent i = new Intent(your random intent Action name).
This should fix you problem

Related

AlarmManager Pending intent not working with broadcast receiver

I am working on an App in which i want to set custom alarm.but after setting a time on alarm nothing happens. i am sharing my code. Please help and guide me if something is missing in my code.
In my MainActivity.java
#Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_ID) {
return new TimePickerDialog(MainActivity.this, kTimePickerListener, hour, min, false);
}
return null;
}
protected TimePickerDialog.OnTimeSetListener kTimePickerListener =
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hourofday, int minutes) {
hour = hourofday;
min = minutes;
Calendar calendar = Calendar.getInstance();
// set selected time from timepicker to calendar
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
Intent myIntent = new Intent(MainActivity.this, ServiceManager.class);
myIntent.putExtra("reqcode", 11);
// A PendingIntent specifies an action to take in the
// future
PendingIntent mPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 11, myIntent, 0);
// set alarm time
alarmManager = (AlarmManager) MainActivity.this
.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), mPendingIntent);
}
};
In My ServiceManager.class
public class ServiceManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int Noti_code = intent.getIntExtra("reqcode",-1);
Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtra("reqcode", Noti_code);
context.startService(myIntent);
}
}
In My NotificationService.class
public class NotificationService extends Service {
private NotificationManager mManager;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#SuppressWarnings({"static-access", "deprecation"})
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
int Noti_Code = intent.getIntExtra("reqcode",-2);
if (Noti_Code == 11) {
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
ShowNotification(getApplicationContext(), mManager);
}
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
In my Manifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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>
<activity android:name=".Activity_detail" />
<activity android:name=".History" />
<service
android:name=".NotificationService"
android:enabled="true"
android:exported="true"
android:stopWithTask="false"/>
<receiver android:name=".ServiceManager"
android:process=":remote"/>
</application>
I am checking through debugging broadcast receiver doesn't called. Please help me if i am missing any thing.
Also i want to know how many pending intent can be fire at a time and will all works one by one?
Thanks.
Refrence links:
AlarmManager wont fire pending intent
http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/
BroadcastReceiver and AlarmManager not working with
AlarmManager is not working after app is closed? - Android
You have your BroadcastReceiver running in another process. If you are setting breakpoints with a debugger you will miss this. If you don't need your BroadcastReceiver running in a separate process, remove the
android:process=":remote"
declaration from the manifest.
See the documentation about how the android:process specifier works.

How to start service after reboot device in Android

I want start my Service after reboot device, I write below codes but when reboot device not start Service.
Manifest:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".alarm1.alarm1"></activity>
<receiver android:name=".alarm1.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name=".alarm1.DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Activity code:
public class alarm1 extends AppCompatActivity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm1);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 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 = 8000;
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, 17);
calendar.set(Calendar.MINUTE, 15);
Toast.makeText(alarm1.this, "Start at 17:11", Toast.LENGTH_SHORT).show();
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 3, pendingIntent);
}
}
AlarmReceiver codes:
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();
}
}
DeviceBootReceiver codes:
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
}
How can I fix this and when reboot device start Service?
Add permissions in Manifest.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Add receiver entry in Manifest.
<receiver android:enabled="true" android:name=".alarm1.DeviceBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
You just need to
Request a permission
and
Explicitly enable the receiver.
The permission goes in the manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
Then when the user sets an alarm (for example your start method), enable the boot receiver:
ComponentName receiver = new ComponentName(context, DeviceBootReceiver.class);
getPackageManager().setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
When the user cancels all alarms, you should disable the boot receiver using a similar piece of code.

Android alarm manager after boot

My alarmmanager code working good. But after boot I can't reschedule my alarm. Can you please tell me where I did mistake?
As you can see in DeviceBootReciever I put the toast. But even this toast doesn't appear. It means I can't listen boot completed.
But I didn't understand the reason. Here I have mentioned my code. Any help will be appreciated!
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(MainActivity.this,AlarmReceiver.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 = 8000; // tekrar süresi milisaniye cinsinden
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 = 8000; // tekrar süresi milisaniye cinsinden
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int hour = 14;
int min= 43;
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
AlarmReciever.class:
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context,"AlarmReceiver Calisiyor",Toast.LENGTH_SHORT).show();
}
}
DeviceBootreciever:
public class DeviceBootReceiver extends BroadcastReceiver {
public void onReceive(Context context,Intent intent){
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
}
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".DeviceBootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>

AlarmManager doesn`t start BroadcastReceiver

I am trying to use BroadcastReceiver and AlarmManager to set a one-shot alarm. I have no idea why isn`t it working. What am I doing wrong? I have no exceptions, no logs about, no suggestions from IDE, everything seems fine, but onReceive method in my BroadcastReceiver is never called.
public void setAlarm(Context mContext) {
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 505151550 ,intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Log.d("alarm",cal.getTime().toString());
}
I am calling it from the activity:
new MyManager().setAlarm(this);
but I have also tried: new MyManager().setAlarm(MyActivity.this);
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("alarm","got into alarm receiver");
}}
in AndroidManifest.xml :
<receiver android:name="com.example.AlarmReceiver"></receiver>
but I have also tried
<receiver android:name=".AlarmReceiver"></receiver>
try like this: it will fire alarm after 30 second.
public class TestAlarmManager extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm(getApplicationContext());
}
public void setAlarm(Context mContext) {
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (30 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + 30 + " seconds",Toast.LENGTH_LONG).show();
}
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent arg1) {
Log.e("alarm","got into alarm receiver");
Toast.makeText(context, "Alarm Started..", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.think.androidteststackoverflow"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher_3"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.think.androidteststackoverflow.TestAlarmManager"
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>

Best way to update Android widget exactly at time?

I have an application with widget, and I don't need to update it periodically, besides one time exactly at 0:00:00.
So, I set android:updatePeriodMillis="0" to save device battery, but what is a best practice to do one update per day?
my solution, based on AlarmManager:
public class Widget extends AppWidgetProvider {
public static String ACTION_AUTO_UPDATE_WIDGET = "ACTION_AUTO_UPDATE_WIDGET";
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent intent = new Intent(Widget.ACTION_AUTO_UPDATE_WIDGET);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 1);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.RTC, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
#Override
public void onDisabled(Context context) {
super.onDisabled(context);
Intent intent = new Intent(Widget.ACTION_AUTO_UPDATE_WIDGET);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.cancel(PendingIntent.getBroadcast(context, 0, intent, 0));
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (ACTION_AUTO_UPDATE_WIDGET.equals(intent.getAction())) {
// do something useful here
Toast.makeText(context, ACTION_AUTO_UPDATE_WIDGET, Toast.LENGTH_LONG).show();
}
}
}
widget in manifest:
<receiver
android:name=".Widget"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="ACTION_AUTO_UPDATE_WIDGET" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_metadata" />
</receiver>

Categories

Resources