Android闹钟不起作用

时间:2011-01-11 17:51:24

标签: android alarmmanager

我几个小时都在苦苦挣扎。我还检查了文档和几个主题。我发现这个代码有两个主题,两个人都说代码工作正常,但不能在我的电脑上运行。第一个Toast出现了,但第二个Toast出现了。有什么问题?

public class HelloAndroid2 extends Activity {  


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

    Intent intent = new Intent(this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

}

 public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

    }
 }

}

3 个答案:

答案 0 :(得分:36)

实际上,您不需要指定操作,因为您在意图中使用了类AlarmReceiver.class

AndroidManifest.xml中,确保在<application>代码中有接收者定义,例如:

<receiver android:name="AlarmReceiver">

编辑: 好的,有两种方法可以使用你的广播接收器。

1)根据您提供的代码,AlarmReceiver.java将包含:

public final class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

    }
 }

HelloAndroid2.java

public class HelloAndroid2 extends Activity {  


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

    Intent intent = new Intent(this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

}

}

像这样,您可以将广播接收器设置为使用AndroidManifest.xml和标记<receiver ...>

2)第二路。通过这种方式,您只能使用1个文件HelloWorld2.java

在您的活动中,创建您的广播接收器并进行注册。

public class HelloWorld2 extends Activity {
    private SharedPreferences prefs;
    private String mName;


    BroadcastReceiver alarmReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();          
        }
    };


    public static final String ACTION_NAME = "com.helloworld.MYACTION";
    private IntentFilter myFilter = new IntentFilter(ACTION_NAME);


    @Override
    protected void onPause() {
        unregisterReceiver(alarmReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        registerReceiver(alarmReceiver, myFilter);
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        registerReceiver(alarmReceiver, myFilter);

        Intent intent = new Intent(ACTION_NAME);        

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();


    }

答案 1 :(得分:11)

我遇到了同样的问题,直到我发现我的广播接收器放在不同的包装上,而不是一般的。

简单地改变了:

<receiver android:name=".AndroidAlarmService" android:enabled="true" >

有:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >

答案 2 :(得分:0)

如果上述答案对您不起作用,那么当AlarmManager触发过期警报时, 会有另一种方式接收任何回调。您只需要检查一下:Intent 的实例化上发送错误的PendingIntent。例如,您希望在其中一个接收方上接听电话onReceive,但是您通过PendingIntentgetActivity实例化getService,但实际上您的意思是{{1} }。

创建getReceiver的实例时,有很多方法可以创建它PendingIntentgetServicegetActivitygetReceiver

如果您想要getForegroundService意图的接收者,那么您:

Activity

如果你想PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 意图的接收者:

BroadcastReceiver

如果你想要一个前景PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*); 意图的接收者:

Service

如果你想要PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*); 意图的接收者:

Service

此外,请确保您的意图指向正确的类。 (例如,为活动,服务等创建意图)。如果您错误地通过这样的话,会收到任何电话:

PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);

我也发布了类似的答案here

HTH