0

我必须通过 AlramManager 调用我的 BroadcastReceiver。

为此,我所做的不起作用:

报警活动.java

public class AlarmActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE, 50);
        calendar.set(Calendar.SECOND, 0);

**Edit : Solution**

        //Intent intent = new Intent(AlarmActivity.this, MyService.class);
        //PendingIntent  pendingIntent = PendingIntent.getService(AlarmActivity.this, 0, intent, 0);
        //alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        //alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis(), pendingIntent);

     AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
     Intent intent1 = new Intent(AlarmActivity.this, MyService.class);
     PendingIntent pendingIntent =   PendingIntent.getBroadcast(AlarmActivity.this, 0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
     am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}

我的服务.java

public class MyService extends BroadcastReceiver {

    @SuppressLint("SimpleDateFormat")
    @Override
    public void onReceive(Context arg0, Intent arg1) {
            Log.e("Broadcast", "Done");
    }
}

Also give alarm permission and receiver

这是我使用但不工作的。你能说出我错过了什么吗?

4

2 回答 2

1

用这个

 AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent intent1 = new Intent(Scheduling.this, Feedback.class);
                PendingIntent pendingIntent =   PendingIntent.getBroadcast(Scheduling.this, 0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(), 86400000, pendingIntent);                  

在清单中添加接收器

于 2013-10-09T07:07:11.293 回答
0

这样做,

public class MyService extends BroadcastReceiver {
private Context mContext;
    @SuppressLint("SimpleDateFormat")
    @Override
    public void onReceive(Context arg0, Intent arg1) {
            StringBuilder msgStr = new StringBuilder();
            Format formatter = new SimpleDateFormat("hh:mm:ss a");
            msgStr.append(formatter.format(new Date()));
            Log.e("Broadcast", "Done");
            mContext = arg0;
    }
}

在 getSystemservice() 写下这段代码,

AlarmManager am = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
于 2013-10-09T06:59:19.557 回答