预定的警报管理器无法运行Android

时间:2015-03-09 10:16:47

标签: android broadcastreceiver android-service alarmmanager repeatingalarm

我正在尝试启动一个在特定时间每天重复的警报服务。我已经经历了关于堆栈溢出的很多线程,但没有运气。我按照了几个教程:http://karanbalkar.com/2013/07/tutorial-41-using-alarmmanager-and-broadcastreceiver-in-android/http://javatechig.com/android/repeat-alarm-example-in-android

我的服务从未开始,我不明白为什么。以下是我的代码:

我的清单文件:

     <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.WAKE_LOCK" />
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application>
        <service android:name="com.paper.DownloadService" android:enabled="true"/>
                  <receiver android:name="com.paper.MyReceiver" ></receiver>
</application>

我的接收器类:

public class MyReceiver extends BroadcastReceiver
{      
    @Override
    public void onReceive(Context rcontext, Intent intent)
    {
        Log.e("Main Activity", "inside on receive of myreceiver");
       Intent service1 = new Intent(rcontext, DownloadService.class);
       rcontext.startService(service1);

    }   
}

我的服务类:

 public class DownloadService extends Service {
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Log.e("Download Service", "CREATED");
    }   
    @SuppressLint({ "SimpleDateFormat", "NewApi" }) @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        Log.e("Download Service", "STARTED");
        return START_NOT_STICKY;
    }
 }

我的主要活动(在创建方法内部):

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 15);
            calendar.set(Calendar.MINUTE, 29);

             Intent myIntent = new Intent(this, MyReceiver.class);
             pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);

             alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                     AlarmManager.INTERVAL_DAY, pendingIntent);

在这里,我试图在每天下午3:29设置闹钟,但该服务并未在当时或任何时间开始。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:11)

以下是我为实现目标所做的工作:

1)在我的清单文件中添加了<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

2)将我的主要活动中的代码更改为:

 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR, 3);
        calendar.set(Calendar.MINUTE, 29);
        calendar.set(Calendar.AM_PM, Calendar.PM);

         Intent myIntent = new Intent(this, MyReceiver.class);
         pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,0);

         alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                 AlarmManager.INTERVAL_DAY, pendingIntent);

希望有人觉得这很有用!