另一个抱怨 AlarmManager(希望有一个快速的解决方案)。我正在使用 Android Emulator进行开发。我找到了一个据称可行的示例,因此我尝试使用它。我做了以下事情:
向清单文件添加了接收器字符串。
<receiver android:name=".SchHandler" android:process=":remote" />
创建了主要活动并使用了它的 onCreate。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bundle bundle = new Bundle(); SchHandler handler = new SchHandler(this, bundle, 1); }
创建了一个 BroadcastReceiver 来创建和收听警报。
public class SchHandler extends BroadcastReceiver { private final String REMINDER_BUNDLE = "ReminderBundle"; public SchHandler (Context context, Bundle extras, int timeoutInSeconds) { Toast.makeText(context, "Scheduling...", Toast.LENGTH_LONG).show(); Log.d("Debug", "Sch"); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, SchHandler.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); Toast.makeText(context, "Time:" + System.currentTimeMillis(), Toast.LENGTH_LONG).show(); alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 2000, 5000, pendingIntent); } @Override public void onReceive(Context context, Intent arg1) { // TODO Auto-generated method stub Log.e(REMINDER_BUNDLE, "Receive"); Toast.makeText(context, "Testing", Toast.LENGTH_LONG).show(); } }
我用set,setRepeating尝试过,没有任何效果。我还应该尝试什么?