帮我解决这个问题。
荫有一些数据在本地数据库, 我每天要检查数据库的日期和具体时间的当前日期,如果数据库的日期相匹配,在状态栏当前日期显示通知
我已经加入我的code,
登入资讯 - > MyNotificationService - > Scheduleclient - > ScheduleServcie - > AlarmTask - > NotifyService流, 在这里,一旦服务已启动,数据库中的数据检索,但条件是不检查的每一天。 我要检查本地数据库中保存的日期和放大器;当前日期,如果两者都是相同的开始通知。
这个环节我都用来参考
Sign_in.Java
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.ProgressBar;
进口android.widget.TextView;
进口android.widget.Toast;
公共类sign_in扩展活动实现OnClickListener {
TextView的签到,ForgotPasword;
私人的EditText电子邮件,密码;
私人按钮登录;
//为共享preferences
公共静态字符串email =;
公共静态字符串密码=;
公共静态字符串storedPassword =;
//这是一个把手,这样我们就可以调用我们的服务方式
私人ScheduleClient scheduleClient;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
的setContentView(R.layout.sign_in);
//会话管理器
会议=新SessionManagerFor_Signin(getApplicationContext());
DB =新DataBaseConnector_forsignUp(本);
DB = DB.open();
签到=(TextView中)findViewById(R.id.textView1);
登录=(按钮)findViewById(R.id.button1);
ForgotPasword =(TextView中)findViewById(R.id.textView4);
EMAIL =(EditText上)findViewById(R.id.editText1);
PASSWORD =(EditText上)findViewById(R.id.editText2);
login.setOnClickListener(本);
//新服务绑定的通知
//创建一个新的服务的客户和我们的活动绑定到该服务
scheduleClient =新ScheduleClient(本);
scheduleClient.doBindService();
}
@覆盖
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.button1:
。电子邮件= EMAIL.getText()的toString();
。密码= PASSWORD.getText()的toString();
session.createLoginSession(电子邮件,密码);
如果(email.equals()|| password.equals()){
//用户不输入的用户名或密码
//显示警告,要求他输入详细信息
alert.showAlertDialog(sign_in.this,登录失败...,请输入电子邮件和密码,假);
} 其他 {
//获取密码表单数据库各自的电子邮件
storedPassword = DB.getSinlgeEntry(电子邮件);
如果(password.equals(storedPassword)){
Toast.makeText(sign_in.this,登录全成,Toast.LENGTH_LONG).show();
意向意图=新的意图(sign_in.this,Page1.class);
startActivity(意向);
完();
//调用商品通知的通知流程
意图myIntent1 =新的意图(sign_in.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(sign_in.this,0,myIntent1,0);
AlarmManager alarmManager1 =(AlarmManager)getSystemService(ALARM_SERVICE);
日历calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis的());
calendar1.add(Calendar.SECOND,40);
alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),pendingintent2);
长time24h = 24 * 60 * 60 * 1000;
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),time24h,pendingintent2);
//获取网络状态
isInternet present = cd1.isConnectingToInternet();
}
}
}
@覆盖
保护无效的onDestroy(){
super.onDestroy();
//关闭数据库
DB.close();
}
//停止通知
@覆盖
保护无效的onStop(){
//当我们的活动停止确保我们也停止连接到服务
//这阻止我们泄露我们的活动到系统*坏*
如果(scheduleClient!= NULL)
scheduleClient.doUnbindService();
super.onStop();
}
}
MyNotificationService的.java
包com.example;
进口java.text.ParseException;
进口java.text.SimpleDateFormat的;
进口的java.util.Calendar;
进口java.util.Date;
进口的java.util.List;
进口com.example.database.DatabaseConnector_forProduct;
进口com.example.service.ScheduleClient;
进口com.example.web.ProductPojo;
进口android.app.Service;
进口android.content.Intent;
进口android.os.IBinder;
进口android.util.Log;
公共类MyNotificationService扩展服务
{
//这是一个把手,这样我们就可以调用我们的服务方式
私人ScheduleClient scheduleClient;
DatabaseConnector_forProduct帮手=新DatabaseConnector_forProduct(本);
公共字符串ExpiryDate;
私人字符串TAG1 =通知.. !!;
@覆盖
公共无效的onCreate()
{
Log.i(TAG1,通知创建);
//新服务绑定的通知
//创建一个新的服务的客户和我们的活动绑定到该服务
scheduleClient =新ScheduleClient(本);
scheduleClient.doBindService();
}
@覆盖
公众的IBinder onBind(意向意图)
{
//Toast.makeText(this,MyAlarmService.onBind(),Toast.LENGTH_LONG).show();
返回null;
}
@覆盖
公共无效的onDestroy()
{
super.onDestroy();
}
@燮pressWarnings(德precation)
@覆盖
公共无效ONSTART(意向意图,诠释startId)
{
super.onStart(意向,startId);
尝试
{
名单< ProductPojo> VAL1 = helper.getAllvalues();
//读所有data..from DB,Toast.LENGTH_LONG).show();
的for(int i = 0; I< val1.size();我++)
{
ExpiryDate = val1.get(ⅰ).getExpiry_Date();
为显示列表视图数据//
字符串DT = ExpiryDate;
SimpleDateFormat的SDF =新的SimpleDateFormat(YYYY-MM-DD);
日历CD = Calendar.getInstance();
尝试
{
cd.setTime(sdf.parse(DT));
}
赶上(ParseException的E)
{
e.printStackTrace();
}
SimpleDateFormat的sdfr =新的SimpleDateFormat(YYYY-MM-DD);
日历CAL = Calendar.getInstance();
日期Warranty_Expired_Date = sdfr.parse(sdfr.format(cd.getTime()));
串dateExpiry = Warranty_Expired_Date.toString();
日期CURRENT_DATE = sdfr.parse(sdfr.format(cal.getTime()));
Log.i(TAG1,dateExpiry +==+ CURRENT_DATE);
如果(Warranty_Expired_Date.equals(CURRENT_DATE))
{
日历CD1 = Calendar.getInstance();
/ * INT DaySpecific = cd1.get(Calendar.DAY_OF_MONTH);
INT month_Specfic = cd1.get(Calendar.MONTH);
INT year_Specific = cd1.get(Calendar.YEAR);
cd1.set(year_Specific,month_Specfic,DaySpecific);
cd1.set(Calendar.HOUR_OF_DAY,5); // 24小时格式
cd1.set(Calendar.MINUTE,30);
cd1.set(Calendar.SECOND,0); * /
//请我们的服务,在这个时间设置闹铃,本次活动会谈,讨论该服务的客户端
scheduleClient.setAlarmForNotification(CD1);
}
其他
{
}
}
}
赶上(例外五)
{
e.printStackTrace();
Log.i(TAG1,捕获到异常);
}
}
@覆盖
公共布尔onUnbind(意向意图)
{
返回super.onUnbind(意向);
}
}
AlarmTask.Java
包com.example.service.task;
进口的java.util.Calendar;
进口android.app.AlarmManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口com.example.service.NotifyService;
公共类AlarmTask实现Runnable {
//选择报警的日期
私人最终日历日期;
// Android系统报警经理
私人最终AlarmManager时;
//你的背景来检索报警经理
私人最终上下文的背景下;
公共AlarmTask(上下文的背景下,日历日){
this.context =背景;
this.am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
this.date =日期;
}
@覆盖
公共无效的run(){
//请求时,报警日期是在我们开始是服务
//我们不开始活动,因为我们只是想弹出一个通知,进入系统吧不是一个完整的活动
意向意图=新的意图(背景下,NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY,真正的);
PendingIntent pendingIntent = PendingIntent.getService(上下文,0,意图,0);
//设置一个报警 - 注意,这个报警将丢失,如果手机关机并重新
am.set(AlarmManager.RTC_WAKEUP,date.getTimeInMillis(),pendingIntent);
/ * // setRepeating(),您可以指定precise自定义的时间间隔 - 在这种情况下,
// 20分钟。
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
1000 * 60 * 20,alarmIntent); * /
}
}
NotifyService.Java
包com.example.service;
进口com.example.Page1;
进口android.R;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.app.Service;
进口android.content.Intent;
进口android.os.Binder;
进口android.os.IBinder;
进口android.util.Log;
公共类NotifyService延伸服务{
/ **
*为客户访问类
* /
公共类的ServiceBinder扩展粘合剂{
NotifyService的getService(){
返回NotifyService.this;
}
}
//唯一的ID来标识的通知。
私有静态最终诠释通知= 123;
//的意图额外的名称,我们可以用它来确定如果此服务已开始创建的通知
公共静态最后弦乐INTENT_NOTIFY =com.blundell.tut.service.INTENT_NOTIFY;
//该系统通知管理
私人NotificationManager MNM;
@覆盖
公共无效的onCreate(){
Log.i(NotifyService,的onCreate());
MNM =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
Log.i(本地服务,收到的起始ID+ startId +:+意图);
//如果此服务被出AlarmTask开始的意图,然后我们要显示我们的通知
如果(intent.getBooleanExtra(INTENT_NOTIFY,FALSE))
showNotification();
//我们如果此服务被终止,因为我们已经交付了通知不在乎
返回START_NOT_STICKY;
}
@覆盖
公众的IBinder onBind(意向意图){
返回mBinder;
}
//这是接收来自客户端的交互的对象
私人最终的IBinder mBinder =新的ServiceBinder();
/ **
*创建一个通知,并显示在操作系统拖下来的状态栏
* /
@燮pressWarnings(德precation)
私人无效showNotification(){
//这是通知中的标题
CharSequence的标题=ALERT!...;
//这是为了在通知使用图标
INT图标= R.drawable.ic_dialog_alert;
//这是通知的滚动文本
CharSequence的文字=ALERT!今天......;
//显示在通知什么时候
很长一段时间= System.currentTimeMillis的();
通知通知=新的通知(图标,文本,时间);
//该PendingIntent来,如果用户选择此通知发布我们的活动
PendingIntent contentIntent = PendingIntent.getActivity(此,0,新意图(本,Page1.class),0);
//设置为显示在通知面板的意见信息。
notification.setLatestEventInfo(这一点,标题,文字,contentIntent);
//清除当它是pressed通知
notification.flags | = Notification.FLAG_AUTO_CANCEL;
//发送通知给系统。
mNM.notify(通知,通知);
//停止服务时,我们完成
stopSelf();
}
}
ScheduleClient.java
包com.example.service;
进口的java.util.Calendar;
进口android.content.ComponentName;
进口android.content.Context;
进口android.content.Intent;
进口android.content.ServiceConnection;
进口android.os.IBinder;
公共类ScheduleClient {
//钩到我们的服务
私人ScheduleService mBoundService;
//背景下启动的服务
私人语境mContext;
//一个标志,如果我们连接到服务或不
私人布尔mIsBound;
公共ScheduleClient(上下文的背景下){
mContext =背景;
}
/ **
*打电话给你的活动连接到您的服务
* /
公共无效doBindService(){
//建立与我们的连接服务
mContext.bindService(新意图(mContext,ScheduleService.class),mConnection,Context.BIND_AUTO_CREATE);
mIsBound = TRUE;
}
私人ServiceConnection mConnection =新ServiceConnection(){
公共无效onServiceConnected(组件名的className,服务的IBinder){
//当已经建立起来,我们的服务的连接,这就是所谓的,
//给我们的服务对象,我们可以用它来与我们的服务交互。
mBoundService =((ScheduleService.ServiceBinder)服务).getService();
}
公共无效onServiceDisconnected(组件名的className){
mBoundService = NULL;
}
};
/ **
*告诉我们的服务,以在指定的日期设置闹钟
*参数℃的日期设置通知
* /
公共无效setAlarmForNotification(日历C){
mBoundService.setAlarm(C);
}
/ **
*当你完成服务调用此方法停止
*释放你的连接和资源
* /
公共无效doUnbindService(){
如果(mIsBound){
//分离我们的现有连接。
mContext.unbindService(mConnection);
mIsBound = FALSE;
}
}
}
ScheduleService.java
包com.example.service;
进口的java.util.Calendar;
进口android.app.Service;
进口android.content.Intent;
进口android.os.Binder;
进口android.os.IBinder;
进口android.util.Log;
进口com.example.service.task.AlarmTask;
公共类ScheduleService延伸服务{
/ **
*为客户访问类
* /
公共类的ServiceBinder扩展粘合剂{
ScheduleService的getService(){
返回ScheduleService.this;
}
}
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
Log.i(ScheduleService,收到的起始ID+ startId +:+意图);
//我们希望这项服务继续运行,直到它被明确停止,因此返回粘。
返回START_STICKY;
}
@覆盖
公众的IBinder onBind(意向意图){
返回mBinder;
}
//这是接收来自客户端的交互的对象。看到
私人最终的IBinder mBinder =新的ServiceBinder();
/ **
*用于显示某个日期报警时,报警称它会弹出一个通知
* /
公共无效setAlarm(日历C){
//这将启动一个新的线程来设置报警
//你想你的任务蹬到一个新的线程来释放用户界面来进行响应
新AlarmTask(这一点,C).RUN();
}
}
解决方案
我找到了答案.Modified在我的code现在它的工作正常,每天早上8点会发送通知。
//通知的过程
意图myIntent1 =新的意图(sign_in.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(sign_in.this,1,myIntent1,1);
AlarmManager alarmManager1 =(AlarmManager)getSystemService(ALARM_SERVICE);
日历calendar1Notify = Calendar.getInstance();
calendar1Notify.setTimeInMillis(System.currentTimeMillis的());
calendar1Notify.set(Calendar.HOUR_OF_DAY,8);
calendar1Notify.set(Calendar.MINUTE,00);
alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(),pendingintent2);
长time24h = 24 * 60 * 60 * 1000;
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(),time24h,pendingintent2);
添加权限清单中
<服务机器人:名称=com.example.service.ScheduleService/>
<服务机器人:名称=com.example.service.NotifyService/>
<服务机器人:名称=com.example.MyNotificationService>
<意向滤光器>
<作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
&所述; /意图滤光器>
< /服务>
Help me to resolve this.
Iam having Some Date in Local Database, Every day I Want to Check Database Date and Current Date in particular time,If database date is matching with current date Show notification in Statusbar
I have added my code,
signin-->MyNotificationService-->Scheduleclient-->ScheduleServcie-->AlarmTask-->NotifyService Flow, Here Once service is Started ,Database data is retrieving But condition Is not Checking every day. I Want to check local database Saved date & current date ,If Both are Same Start the notification.
This link i Have used for Reference
Sign_in.Java
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class sign_in extends Activity implements OnClickListener {
TextView Signin, ForgotPasword;
private EditText EMAIL, PASSWORD;
private Button login;
// for Shared preferences
public static String email = "";
public static String password = "";
public static String storedPassword = "";
// This is a handle so that we can call methods on our service
private ScheduleClient scheduleClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sign_in);
// Session Manager
session = new SessionManagerFor_Signin(getApplicationContext());
DB = new DataBaseConnector_forsignUp(this);
DB = DB.open();
Signin = (TextView) findViewById(R.id.textView1);
login = (Button) findViewById(R.id.button1);
ForgotPasword = (TextView) findViewById(R.id.textView4);
EMAIL = (EditText) findViewById(R.id.editText1);
PASSWORD = (EditText) findViewById(R.id.editText2);
login.setOnClickListener(this);
//new Service bind for Notification
// Create a new service client and bind our activity to this service
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
email = EMAIL.getText().toString();
password = PASSWORD.getText().toString();
session.createLoginSession(email, password);
if (email.equals("") || password.equals("")) {
// user didn't entered user name or password
// Show alert asking him to enter the details
alert.showAlertDialog(sign_in.this, "Login failed...","Please Enter Email and Password", false);
} else {
// fetch the Password form database for respective Email
storedPassword = DB.getSinlgeEntry(email);
if (password.equals(storedPassword)) {
Toast.makeText(sign_in.this, "Login Successfull",Toast.LENGTH_LONG).show();
Intent intent = new Intent(sign_in.this, Page1.class);
startActivity(intent);
finish();
//Calling the Notication for Notification Process
Intent myIntent1 = new Intent(sign_in.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(sign_in.this, 0,myIntent1, 0);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
calendar1.add(Calendar.SECOND, 40);
alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(), pendingintent2);
long time24h = 24*60*60*1000;
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(), time24h,pendingintent2);
// get Internet status
isInternetPresent = cd1.isConnectingToInternet();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Close The Database
DB.close();
}
//Stop the Notification
@Override
protected void onStop() {
// When our activity is stopped ensure we also stop the connection to the service
// this stops us leaking our activity into the system *bad*
if(scheduleClient != null)
scheduleClient.doUnbindService();
super.onStop();
}
}
MyNotificationService .Java
package com.example;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.example.database.DatabaseConnector_forProduct;
import com.example.service.ScheduleClient;
import com.example.web.ProductPojo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyNotificationService extends Service
{
// This is a handle so that we can call methods on our service
private ScheduleClient scheduleClient;
DatabaseConnector_forProduct helper=new DatabaseConnector_forProduct(this);
public String ExpiryDate;
private String TAG1 = "Notify..!!";
@Override
public void onCreate()
{
Log.i(TAG1, "Notification Created");
//new Service bind for Notification
// Create a new service client and bind our activity to this service
scheduleClient = new ScheduleClient(this);
scheduleClient.doBindService();
}
@Override
public IBinder onBind(Intent intent)
{
//Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
try
{
List<ProductPojo> val1 = helper.getAllvalues();
// "Reading all data..from Db ", Toast.LENGTH_LONG).show();
for (int i=0;i<val1.size();i++)
{
ExpiryDate = val1.get(i).getExpiry_Date();
//for Showing List view Data
String dt = ExpiryDate;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
try
{
cd.setTime(sdf.parse(dt));
}
catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat sdfr = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Date Warranty_Expired_Date = sdfr.parse(sdfr.format(cd.getTime()));
String dateExpiry=Warranty_Expired_Date.toString();
Date Current_Date = sdfr.parse(sdfr.format(cal.getTime()));
Log.i(TAG1,dateExpiry+"=="+Current_Date);
if(Warranty_Expired_Date.equals(Current_Date))
{
Calendar cd1 = Calendar.getInstance();
/*int DaySpecific = cd1.get(Calendar.DAY_OF_MONTH);
int month_Specfic = cd1.get(Calendar.MONTH);
int year_Specific = cd1.get(Calendar.YEAR);
cd1.set(year_Specific, month_Specfic, DaySpecific);
cd1.set(Calendar.HOUR_OF_DAY, 5);//24 Hour Format
cd1.set(Calendar.MINUTE, 30);
cd1.set(Calendar.SECOND, 0);*/
// Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
scheduleClient.setAlarmForNotification(cd1);
}
else
{
}
}
}
catch (Exception e)
{
e.printStackTrace();
Log.i(TAG1,"Exception Caught");
}
}
@Override
public boolean onUnbind(Intent intent)
{
return super.onUnbind(intent);
}
}
AlarmTask.Java
package com.example.service.task;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.example.service.NotifyService;
public class AlarmTask implements Runnable{
// The date selected for the alarm
private final Calendar date;
// The android system alarm manager
private final AlarmManager am;
// Your context to retrieve the alarm manager from
private final Context context;
public AlarmTask(Context context, Calendar date) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.date = date;
}
@Override
public void run() {
// Request to start are service when the alarm date is upon us
// We don't start an activity as we just want to pop up a notification into the system bar not a full activity
Intent intent = new Intent(context, NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY, true);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
// Sets an alarm - note this alarm will be lost if the phone is turned off and on again
am.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
/*// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);*/
}
}
NotifyService.Java
package com.example.service;
import com.example.Page1;
import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class NotifyService extends Service {
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
// Unique id to identify the notification.
private static final int NOTIFICATION = 123;
// Name of an intent extra we can use to identify if this service was started to create a notification
public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
// The system notification manager
private NotificationManager mNM;
@Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// If this service was started by out AlarmTask intent then we want to show our notification
if(intent.getBooleanExtra(INTENT_NOTIFY, false))
showNotification();
// We don't care if this service is stopped as we have already delivered our notification
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients
private final IBinder mBinder = new ServiceBinder();
/**
* Creates a notification and shows it in the OS drag-down status bar
*/
@SuppressWarnings("deprecation")
private void showNotification() {
// This is the 'title' of the notification
CharSequence title = " Alert...!";
// This is the icon to use on the notification
int icon = R.drawable.ic_dialog_alert;
// This is the scrolling text of the notification
CharSequence text = "Alert Today..!";
// What time to show on the notification
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, text, time);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Page1.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
// Clear the notification when it is pressed
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Send the notification to the system.
mNM.notify(NOTIFICATION, notification);
// Stop the service when we are finished
stopSelf();
}
}
ScheduleClient.java
package com.example.service;
import java.util.Calendar;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
public class ScheduleClient {
// The hook into our service
private ScheduleService mBoundService;
// The context to start the service in
private Context mContext;
// A flag if we are connected to the service or not
private boolean mIsBound;
public ScheduleClient(Context context) {
mContext = context;
}
/**
* Call this to connect your activity to your service
*/
public void doBindService() {
// Establish a connection with our service
mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with our service has been established,
// giving us the service object we can use to interact with our service.
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
/**
* Tell our service to set an alarm for the given date
* @param c a date to set the notification for
*/
public void setAlarmForNotification(Calendar c){
mBoundService.setAlarm(c);
}
/**
* When you have finished with the service call this method to stop it
* releasing your connection and resources
*/
public void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContext.unbindService(mConnection);
mIsBound = false;
}
}
}
ScheduleService.java
package com.example.service;
import java.util.Calendar;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.example.service.task.AlarmTask;
public class ScheduleService extends Service {
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
ScheduleService getService() {
return ScheduleService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("ScheduleService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly stopped, so return sticky.
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients. See
private final IBinder mBinder = new ServiceBinder();
/**
* Show an alarm for a certain date when the alarm is called it will pop up a notification
*/
public void setAlarm(Calendar c) {
// This starts a new thread to set the alarm
// You want to push off your tasks onto a new thread to free up the UI to carry on responding
new AlarmTask(this, c).run();
}
}
解决方案
I found the Answer .Modified in my code Now its working fine Every Day 8 Am Will Send Notification
// for Notification Process
Intent myIntent1 = new Intent(sign_in.this,MyNotificationService.class);
pendingintent2 = PendingIntent.getService(sign_in.this, 1,myIntent1, 1);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar1Notify = Calendar.getInstance();
calendar1Notify.setTimeInMillis(System.currentTimeMillis());
calendar1Notify.set(Calendar.HOUR_OF_DAY, 8);
calendar1Notify.set(Calendar.MINUTE, 00);
alarmManager1.set(AlarmManager.RTC_WAKEUP,calendar1Notify.getTimeInMillis(), pendingintent2);
long time24h = 24*60*60*1000;
alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar1Notify.getTimeInMillis(),time24h,pendingintent2);
Add Permission in manifest
<service android:name="com.example.service.ScheduleService" />
<service android:name="com.example.service.NotifyService" />
<service android:name="com.example.MyNotificationService" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</service>