1

另一个抱怨 AlarmManager(希望有一个快速的解决方案)。我正在使用 Android Emulator进行开发。我找到了一个据称可行的示例,因此我尝试使用它。我做了以下事情:

  1. 向清单文件添加了接收器字符串。

     <receiver android:name=".SchHandler" android:process=":remote" />
    
  2. 创建了主要活动并使用了它的 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);
    }
    
  3. 创建了一个 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();
        }
    }
    

我用setsetRepeating尝试过,没有任何效果。我还应该尝试什么?

4

2 回答 2

0

我将接收器的名称修改为完全限定的版本,结果发现我使用了它的包名而不是类名。正确设置后,我得到了它的工作。

于 2013-02-25T19:22:49.437 回答
0

邪恶的根源可能android:process=":remote"在您的<receiver>标签中。尝试删除它。

于 2013-01-18T08:35:59.240 回答