12

我已经为此苦苦挣扎了好几个小时。我还检查了文档和几个主题。我在两个主题中找到了这段代码,两个人都说代码运行良好,但在我的电脑上却不行。第一个 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();

    }
 }

}

4

3 回答 3

39

实际上,您不需要指定操作,因为您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();


    }
于 2011-01-11T18:18:55.947 回答
11

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

简单地改变了:

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

为了:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
于 2013-01-30T22:06:12.490 回答
0

如果上面的答案对您不起作用,那么还有另一种方法可以在触发过期警报时 接收任何回调。AlarmManager您只需要检查这一点:通过IntentPendingIntent. 例如,您想在onReceive您的一个接收器上接听电话,但您实例化了一个PendingIntentvia getActivityor getService,但您的实际意思是getReceiver.

创建 的实例时PendingIntent,有多种创建方法(getService, getActivity, getReceiver, 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_*);

另外,请确保您的意图指向正确的类。(例如,为 Activity、Service 等创建意图)。如果您像这样错误地通过,您将不会接到任何电话:

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

我也在这里发布了类似的答案。

高温高压

于 2017-07-14T01:50:07.927 回答