警报服务android无法正常工作

时间:2016-02-11 09:40:52

标签: java android

所以即时尝试在我的Android应用中设置预定闹钟。所以设置报警服务如下。请告诉我它是否正确,因为它不起作用。

Context context=getApplicationContext();
    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, MyBroadcastReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            1000 * 60, alarmIntent);

以上是主要方法。现在我创建了一个接收器类

public class MyBroadcastReceiver  extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Calendar c = Calendar.getInstance();
        hour=c.get(Calendar.HOUR_OF_DAY);
        minute=c.get(Calendar.MINUTE);
        Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute);
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
        String date = sdf.format(calendar.getTime());
        String str=date.charAt(0)+""+date.charAt(1)+""+date.charAt(3)+""+date.charAt(4);
        minochaDevicePolicyManager.resetPassword(str,0);
    }
}

这不起作用。为什么 我希望onReceive每分钟运行一次。代码好吗?

1 个答案:

答案 0 :(得分:1)

您必须在receiver中添加AndroidManifext.xml才能收到警报。

<manifest ...>
    <application ...>
        ...
        <receiver android:name=".MyBroadcastReceiver" />
    </application>    
</manifest>