【问题标题】:Starting a service via Alarm does not work通过警报启动服务不起作用
【发布时间】:2012-06-04 07:13:00
【问题描述】:

我有以下广播接收器,当手机启动时会被调用 (Bootreciever.java),我正在尝试使用重复警报启动间歇性运行的服务。

public void onReceive(Context context, Intent intent) {

    Toast.makeText(context,"Inside onRecieve() :D" , Toast.LENGTH_LONG).show();
    Log.d(getClass().getName(), "Inside onRecieve() :D");


    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);


    Intent i = new Intent(context, MyService.class);

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 3000, 1000, pi);

}

基本上,我将重复警报设置为触发 3 秒,之后每隔一秒触发一次。启动完成广播收到就好了 - 但服务没有启动。 MyService.java 看起来像这样:

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this, "Hello from MyService!", Toast.LENGTH_LONG);

        Log.d(getClass().getName(), "Hello from MyService!");

        stopSelf();

        return super.onStartCommand(intent, flags, startId);
    }

启动服务时我做错了什么?

Logcat 没有告诉我任何信息,并且我在清单中定义了该服务。使用 startService() 调用时,该服务本身可以工作,但在 PendingIntent 中使用时似乎失败。

【问题讨论】:

  • 尝试 context.getApplicationContext() 代替 PendingIntent 语法中的上下文。

标签: android service broadcastreceiver android-pendingintent repeatingalarm


【解决方案1】:

PendingIntent.getService()代替PendingIntent.getBroadcast()

【讨论】:

  • 哎哟!错误的方法调用。感谢您指出了这一点。现在可以使用了。
  • 请不要发布单行答案。解释问题。 -1
  • @Manishearth 我不想。
【解决方案2】:

我认为您应该检查您的持续时间,因为 android 对频繁触发的警报有点挑剔。为了启动服务,最好的方法是设置警报以将意图广播到广播接收器,然后让它启动您的服务。

示例 1 接收器:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class QoutesReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // log instruction
        Log.d("SomeService", "Receiving Broadcast, starting service");
        // start the qoute service

        Intent newService = new Intent(context, SomeService.class);
        context.startService(newService);
    }

}

示例 2 设置闹钟的函数(此示例每 2 分钟运行一次):

public void setAlarm(Context context) {


        // Get the current time
        Calendar currTime = Calendar.getInstance();

        // Get the intent (start the receiver) 
        Intent startReceiver = new Intent(context, MyReceiver.class);

        PendingIntent pendReceiver = PendingIntent.getBroadcast(context, 0,
                startReceiver, PendingIntent.FLAG_CANCEL_CURRENT);

        // call the Alarm service
        AlarmManager alarms = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        //Setup the alarm

        alarms.setRepeating(AlarmManager.RTC_WAKEUP,
                currTime.getTimeInMillis() + (2 * 60 * 1000), 2 * 60 * 1000,
                pendReceiver);
    }

【讨论】:

    猜你喜欢
    • 2010-11-11
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2012-05-04
    相关资源
    最近更新 更多