BroadcastReceiver和AlarmManager Android

青春须早为,岂能长少年。这篇文章主要讲述BroadcastReceiver和AlarmManager Android相关的知识,希望能为你提供帮助。
我正在尝试使用BroadcastReceiver的警报管理器。我尝试使用Tutorial: System Services and BroadcastReceiver中给出的示例。但是当我在结束后运行示例时,吐司不会出现。代码如下:
我的主要活动:

public class AlarmActivity extends Activity {/** Called when the activity is first created. */@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm); }public void startAlert(View view) { EditText text = (EditText) findViewById(R.id.time); int i = Integer.parseInt(text.getText().toString()); Intent intent = new Intent(this, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent); Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show(); }}

我的broadCastReceiver:
public class MyBroadcastReceiver { public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Don't panic but your time is up!!!!.", Toast.LENGTH_LONG).show(); // Vibrate the mobile phone Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2000); }}

我的主要布局:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > < EditText android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="Number of seconds" android:inputType="numberDecimal" > < /EditText> < Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startAlert" android:text="Start Counter" > < /Button> < /LinearLayout>

和manifestfile:
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcastreceiver" android:versionCode="1" android:versionName="1.0" > < uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> < uses-permission android:name="android.permission.VIBRATE" > < /uses-permission> < application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > < activity android:name="com.example.broadcastreceiver.AlarmActivity" android:label="@string/app_name" > < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < receiver android:name="com.example.android_alarm.MyBroadcastReceiver" > < /receiver> < /application> < /manifest>

答案接收器必须从BroadcastReceiver扩展
public class MyBroadcastReceiver extends BroadcastReceiver { // ... }

还要确保清单文件中的接收者名称是正确的。
另一答案你的MyBroadcastReceiver类放错了这段代码
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG).show(); // Vibrate the mobile phone /*Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(2000); */ } }

只需要添加“extends BroadcastReceiver”并获取onReceive()覆盖方法。
另一答案你只是忘了重写OnReceive()方法。 public void onReceive(Context context, Intent intent)是BroadcastReceiver的预定义方法,当你使用它时,你必须@Override如下,
public class MyBroadcastReceiver extends BroadcastReceiver { @Override// You forgot this line public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Don't panik but your time is up!!!!.",Toast.LENGTH_LONG).show(); } }

另一答案【BroadcastReceiver和AlarmManager Android】你已经创建了一个你没有在任何地方调用的startAlert函数。所以首先在onCreate中调用该方法,然后你会收到一个toast。

    推荐阅读