简体   繁体   English

在特定日期在Android中设置通知提醒

[英]Set notification reminder in android on particular date

Hi I am trying to set reminder notification when my activity is created but , I am getting null pointer error when scheduleClient.setAlarmForNotification(c) is called . 嗨,我正在尝试在创建活动时设置提醒通知,但是,在调用scheduleClient.setAlarmForNotification(c)时出现空指针错误。

These is my main activity where im calling error 这些是我的主要活动,即时通讯发生错误

MainActivity 主要活动

   public class MainActivity extends Activity  {
// This is a handle so that we can call methods on our service
private ScheduleClient scheduleClient;
// This is the date picker used to select the date for our notification

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create a new service client and bind our activity to this service
    scheduleClient = new ScheduleClient(this);
    scheduleClient.doBindService();
    int day =3;
    int month = 3;
    int year = 2014;
    // Create a new calendar set to the date chosen
    // we set the time to midnight (i.e. the first minute of that day)
    Calendar c = Calendar.getInstance();
    c.set(year, month, day);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.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(c);
    //Notify the user what they just did
    Toast.makeText(this, "Notification set for: "+ c.get(Calendar.DAY_OF_MONTH) +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();

}

@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();
}

} }

AlarmTask.java AlarmTask.java

 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, date.getTimeInMillis(), pendingIntent);
}

} }

SchedulecClient SchedulecClient

    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;
}

/**
 * When you attempt to connect to the service, this connection will be called with the result.
 * If we have successfully connected we instantiate our service object so that we can call methods on it.
 */
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 ScheduleService

 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();
}

} }

Make one function. 使一个功能。

public void onDateSelectedClickEvent(View v) {
    // Get the date from our datepicker
    int day = 3;
    int month = 2;
    int year = 2014;
    // Create a new calendar set to the date chosen
    // we set the time to midnight (i.e. the first minute of that day)
    Calendar c = Calendar.getInstance();
    c.set(year, month, day);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.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(c);
    // Notify the user what they just did
    Toast.makeText(
            this,
            "Notification set for: " + day + "/" + (month + 1) + "/" + year,
            Toast.LENGTH_SHORT).show();
}

Now in your xml file set call this function your button on which you want to call this event in xml file. 现在,在您的xml文件集中,将此函数调用您要在xml文件中调用此事件的按钮。 Like 喜欢

    <Button
    android:id="@+id/sel_btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="onDateSelectedClickEvent"
    android:text="Notification" />

Also remove that code from onCreate() method. 还要从onCreate()方法中删除该代码。 Must be insure that your all Services registered in your AndroidManifest.xml file. 必须确保所有服务都已在AndroidManifest.xml文件中注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM