0

我正在一个Android应用程序中工作,我想在换班期间显示警报,比如说每 8 小时一次。我该怎么做?

我已经尝试了很多,所有人都在解决一些重大问题。我可以得到一个教程或提示我如何做到这一点。我正在给我尝试过的东西

    public class ShiftConfirmService extends Service 
       {

        private NotificationManager mManager;

        @Override
      public IBinder onBind(Intent arg0)
       {
       // TODO Auto-generated method stub
            return null;
      }

         @Override
        public void onCreate() 
       {
    // TODO Auto-generated method stub  
    super.onCreate();
 }

   @SuppressWarnings("static-access")
     @Override
     public void onStart(Intent intent, int startId)
      {
   super.onStart(intent, startId);
   Intent intent1 = new Intent(this, ShiftAlert.class);
   intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent1);

}

@Override
public void onDestroy() 
{
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

4

3 回答 3

1

您可以使用警报管理器

还有一些关于它的教程如下

1) http://www.techrepublic.com/blog/android-app-builder/use-androids-alarmmanager-to-schedule-an-event/

2) http://www.javacodegeeks.com/2012/09/android-alarmmanager-tutorial.html

3)您可以尝试此代码片段进行 24 小时测试

Intent myIntent = new Intent(ThisApp.this , myService.class);     
       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
       pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);

       Calendar calendar = Calendar.getInstance();
           calendar.set(Calendar.HOUR_OF_DAY, 12);
       calendar.set(Calendar.MINUTE, 00);
       calendar.set(Calendar.SECOND, 00);

      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

4) http://learnandroideasily.blogspot.in/2013/05/android-alarm-manager_31.html

5) http://khurramitdeveloper.blogspot.in/2013/06/android-alarm-manager-to-start-service.html

于 2013-10-01T07:48:10.793 回答
0

您还可以在特定时间使用待定意图。

Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

更多Android 通知 - 教程

于 2013-10-01T07:55:20.657 回答
0

在您开始服务的活动中设置以下代码:

Intent myIntent = new Intent(ThisApp.this , myService.class);     
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(ThisApp.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 8*60*60*1000 , pendingIntent);  //set repeating every 8 hours

还有一件事,如果您的设备关闭,您需要制作一个接收器来启动您的服务。为此,您还需要对 Android 清单文件进行一些更改。

public class MyScheduleReceiver extends BroadcastReceiver {

  // Restart service every 30 seconds
  private static final long REPEAT_TIME = 1000 * 300;

  @Override
  public void onReceive(Context context, Intent intent) {

        AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, YourService.class);
        PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, 0);
        Calendar cal = Calendar.getInstance();
        service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 8*60*60*1000, pending); 

    // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    // REPEAT_TIME, pending);

  }
} 

Android 清单文件:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.fleetcare.MyScheduleReceiver" >
           <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
于 2013-10-01T07:55:23.313 回答