Reputation: 77
Hello I am building a news application in android, so that every time a new data is entered into database user must receive a notification in the notification bar. So after research I found that I have to use broadcast receiver and alarm manager. But what I am not able to understand is that when the application is closed how it will know that a new data is entered? and how to make the alarm manager activate the application for checking new data let's say every 5 minutes? Thank you.
Upvotes: 1
Views: 5273
Reputation: 46
This project can show the notification every 10000 seconds after setting the time using AlarmManager.
AlarmActivityActivity.java:
package co.in.technoguys.AlarmActivity;
import java.util.Calendar;
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.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmActivityActivity extends Activity {
int notifyID=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAlert(View view) {
TimePicker tp=(TimePicker)findViewById(R.id.timePicker1);
DatePicker dp=(DatePicker)findViewById(R.id.datePicker1);
Calendar Cal = Calendar.getInstance();
Cal.set(Calendar.YEAR, dp.getYear());
Cal.set(Calendar.MONTH, dp.getMonth());
Cal.set(Calendar.DAY_OF_MONTH, dp.getDayOfMonth());
Cal.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
Cal.set(Calendar.MINUTE, tp.getCurrentMinute());
Cal.set(Calendar.SECOND, 00);
long when=Cal.getTimeInMillis();
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,when,20000,pendingIntent);
Toast.makeText(this, "Notification Triggered on"+tp.getCurrentHour()+":"+tp.getCurrentMinute(),
Toast.LENGTH_LONG).show();
}
}
MyBroadcastReceiver.java:
package co.in.technoguys.AlarmActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
int notifyID=1;
@Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent();
PendingIntent pi=PendingIntent.getBroadcast(context,0,i,0);
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification(R.drawable.ic_launcher,"Notification Triggered",System.currentTimeMillis());
CharSequence from="Remainder";
CharSequence message="Meeting Starts at 9 A.M";
notify.setLatestEventInfo(context, from, message, pi);
nm.notify(notifyID,notify);
}
}
main.xml:
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/timePicker1"
android:layout_alignLeft="@+id/timePicker1"
android:layout_marginBottom="27dp" />
<Button
android:id="@+id/startAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_below="@+id/timePicker1"
android:layout_marginTop="36dp"
android:onClick="startAlert"
android:text="@string/count" />
</RelativeLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.in.technoguys.AlarmActivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.VIBRATE" >
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AlarmActivityActivity"
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="MyBroadcastReceiver" >
</receiver>
</application>
</manifest>
Upvotes: 2
Reputation: 671
If I understand you correctly, you don't need to run the whole application in order to check for new data, as it should be done in a separate background task.
What you need is an alarm registered within AlarmManager and a BroadcastReceiver that will get invoked when the alarm is fired. The receiver checks for new data, and if the condition is met, it executes the intent that will start the notification activity.
Please keep in mind, that if the database access requires more time, it'd better be done in a service started by the receiver.
You can save some coding (and solve some common problems) by using cwac-wakeful ( https://github.com/commonsguy/cwac-wakeful ), which will also make sure that your database-checking service stays awake as long as it does its job. It's very well documented and has several examples available.
Some theory worth reading can be found at: http://www.vogella.com/articles/AndroidServices/article.html . It is mostly about services, but has some explanations about alarms too.
Upvotes: 1