3

编辑在我的清单中添加这一行解决了我的问题(Service创建得很好)。

<service android:name=".TimersService" >

邮政

我目前正在尝试实现警报以通知用户倒计时已完成。我有一种方法createAlarm()可以通过AlarmManager. 此方法当前在 Fragment 内部调用。它看起来像这样:

private final void createAlarm(String name, long milliInFuture) {

        Intent myIntent = new Intent(getActivity().getApplication(),
                TimersService.class);

        AlarmManager alarmManager = (AlarmManager) getActivity()
                .getSystemService(Context.ALARM_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getService(getActivity()
                .getApplication(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        alarmManager.set(AlarmManager.RTC_WAKEUP,
                milliInFuture, pendingIntent); 

    }

我希望这种方法能够添加警报。即使设备处于睡眠模式,也应该调用警报。应该在某个时间milliInFuture(即System.currentTimeMillis()+ 某个时间)调用它。当警报响起时,它应该启动一个服务。服务如下。这Service应该只做一件事:通知用户警报已经结束。我的Service班级如下:

public class TimersService extends Service {

    private NotificationManager mNM;
    private int NOTIFICATION = 3456;


    public class LocalBinder extends Binder {
        TimersService getService() {
            return TimersService.this;
        }
    }

    @Override
    public void onCreate() {
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
            return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        mNM.cancel(NOTIFICATION);
        Toast.makeText(this, "Alarm", Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

        private final IBinder mBinder = new LocalBinder();

    private void showNotification() {

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
        builder.setSmallIcon(R.drawable.clock_alarm);
        builder.setContentTitle("Time is up");
        builder.setContentText("SLIMS");
        builder.setVibrate(new long[] { 0, 200, 100, 200 });
        final Notification notification = builder.build(); 

        mNM.notify(NOTIFICATION, notification);
        NOTIFICATION += 1;
    }

}

当我运行我的代码时,我的方法 createAlarm 被调用。但是我的服务永远不会被创建。我根据 Alexander's Fragotsis's one found here编写了这段代码。我的Service课程灵感来自Service 类的 Android 参考资料。

知道为什么我Service没有被调用吗?Manifest关于警报、服务或通知,我应该写些什么吗?

感谢您的帮助

何和我将不胜感激有关我的代码的任何建议。如果您知道在固定时间后通知用户的更简单方法,请告诉我!

4

1 回答 1

3

由于您所做的只是通知您的用户一次,因此服务并不是最好的方法。服务旨在在后台工作。通知并不是真正适合服务的工作类型——它太短了。因此,我建议您改用 BroadcastReceiver。

类应该是这样的:

public class TimerReceiver extends BroadcastReceiver {
  private static final int NOTIFICATION = 3456; 

/*since you're always doing a 1-time notification, we can make this final and static, the number
 won't change. If you want it to change, consider using SharedPreferences or similar to keep track 
 of the number. You would have the same issue with a Service since you call stopself() and so,
 you would delete the object every time.*/

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

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.clock_alarm);
    builder.setContentTitle("Time is up");
    builder.setContentText("SLIMS");
    builder.setVibrate(new long[] { 0, 200, 100, 200 });
    final Notification notification = builder.build(); 

    mNM.notify(NOTIFICATION, notification);
  }

要调用接收器,您需要将 Intent 更改为指向新类,并且getService()需要将getBroadcast(). 因此这

Intent myIntent = new Intent(getActivity().getApplication(),
                TimersService.class);


PendingIntent pendingIntent = PendingIntent.getService(getActivity()
                .getApplication(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

需要是

Intent myIntent = new Intent(getActivity().getApplication(),
                TimerReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity()
                .getApplication(), 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

此外,您应该能够安全地更改getActivity().getApplication()getActivity()

最后你需要一个清单声明:

<receiver android:name=".TimerReceiver" ></receiver>
于 2013-01-21T20:51:57.803 回答