Android服务和AlarmManager粉碎

时间:2014-08-02 07:59:45

标签: android service notifications broadcastreceiver alarmmanager

我正在尝试创建一个将在后台运行的服务,并每隔10秒在状态栏中发送一个通知(仅用于测试)。

我在这里查看了很多帖子,但仍无法找到帮助。

出于某种原因,当我调用AlarmManager来运行服务时,应用程序会崩溃。 请指教。

这是“主类”代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_screen);
    MyAlarmManager.scheduleAlarms(this);

这是AlarmManager代码:

public class MyAlarmManager extends BroadcastReceiver {

  private static final int PERIOD=1000*10; // 15 minutes
  private static final int INITIAL_DELAY=500; // 5 seconds

  @Override
  public void onReceive(Context ctxt, Intent i) {
      scheduleAlarms(ctxt);
  }

  static void scheduleAlarms(Context ctxt) {
    AlarmManager mgr= (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(ctxt, MyService.class);
    PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);
    Log.i("My Log", "Alarm Manager Started ......... Alarm Manager Started");

    mgr.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + INITIAL_DELAY, PERIOD, pi);
  }
}

这是“服务”代码:

public class MyService extends Service {
private NotificationManager mNM;
private final IBinder mBinder = new LocalBinder();
private int NOTIFICATION = R.string.service_started;

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

@Override
public void onCreate() {
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Log.i("My Log", "Received onCreate Service !!!!!!!!!!!!!!!!!!!");

    showNotification();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("My Log", "Received onStartCommand id " + startId + ": " + intent);
    Toast.makeText(this, R.string.service_started, Toast.LENGTH_SHORT).show();
    showNotification();

    // This service will continue running until it is explicitly stopped, so return sticky.
    return START_STICKY;
}

@Override
public void onDestroy() {
    // Cancel the persistent notification.
    mNM.cancel(NOTIFICATION);
    Toast.makeText(this, R.string.service_stopped, Toast.LENGTH_SHORT).show();
}

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

private void showNotification() {

    CharSequence text = getText(R.string.service_started);

    Notification noti = new Notification.Builder(getBaseContext())
        .setContentTitle(getText(R.string.noto))
        .setContentText("Test Notification ON")
        .setSmallIcon(R.drawable.ic_launcher)
        .build();
}
}

这是我的日志

08-02 11:09:48.872: E/AndroidRuntime(9550): Caused by: java.lang.ClassCastException: com.homeapps4u.sec_ticker_rss.MyService cannot be cast to android.content.BroadcastReceiver
08-02 11:09:48.872: E/AndroidRuntime(9550):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
08-02 11:09:48.872: E/AndroidRuntime(9550):     ... 10 more

请告诉我我错在哪里。

非常感谢。

1 个答案:

答案 0 :(得分:-1)

谢谢,我应该使用:

        PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);

在MyAlarmManager上。