我的背景IntentService在模拟器上每1分钟后完全触发但在实际设备上根本没有运行,有人可以告诉我为什么吗? 我使用的手机是Android Infinix。
在我的MainActivity的onCreate方法中,我有:
AlarmManager alarmMgr = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, RemoveDownloadService.class);
intent.setData(Uri.parse("First"));
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
// Set the alarm to start at 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 32);
// Repeat every 24-hours
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 1, pendingIntent);
我的Manfiest文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.macbookpro.videodownload">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DownloadListActivity"
android:screenOrientation="portrait">
</activity>
<service
android:name=".RemoveDownloadService"
android:exported="false"/>
</application>
IntentService类:
public class RemoveDownloadService extends IntentService {
public static final String LOG_TAG = "RemoveDownloadService";
public RemoveDownloadService() {
super(LOG_TAG);
}
@Override
protected void onHandleIntent(Intent workIntent) {
String str = workIntent.getDataString();
ArrayList<VideoDownloadEntity> arrDownloads = new ArrayList<>();
arrDownloads = DatabaseHandler.getHelper(getApplicationContext()).getVideoDowloads();
Toast toast = Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String nowDateStr = dateFormat.format(Calendar.getInstance().getTime());
try {
Date nowDate = dateFormat.parse(nowDateStr);
if(arrDownloads.size()>0)
{
for(VideoDownloadEntity downloadEntity: arrDownloads)
{
Date downloadedDate = dateFormat.parse(downloadEntity.getVideoDownloadDate());
long diff = downloadedDate.getTime() - nowDate.getTime();
long dateDaysDifference = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
System.out.println ("Days: " + dateDaysDifference);
// If the downloaded Video is older than 7 days then Delete it
if(dateDaysDifference > 7l)
MediaDownloadManager.getInstance(getBaseContext()).deleteMediaFile(downloadEntity);
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
尝试从
更改结束标记<service
android:name=".RemoveDownloadService"
android:exported="false">
</service>
到
requestCode
对于运行android 4.3的某些设备,int requestCode = 1;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
应不为0
stopSelf();
也可能需要将onHandleIntent()
添加到Fecha