我正在尝试启动一个每天在特定时间重复的闹钟服务。我在stackoverflow网站上查看了很多关于此问题的帖子,但没有成功。我按照一些教程操作: 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;
}
}
我的主要活动(在On Create方法中):
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设置我的闹钟,但该服务在那个时间或任何时间都没有启动。感谢您的帮助!