내 백그라운드 IntentService가 에뮬레이터에서 1 분마다 완벽하게 트리거되지만 실제로 장치에서 실행되지 않습니다. 아무도 그 이유를 말할 수 있습니까? 내가 사용하는 전화는 Android Infinix입니다. 내 MainActivity의에서 onCreate 방법에서IntentService는 에뮬레이터에서 작동하지만 실제 장치에서는 작동하지 않습니다.
난이 :
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();
}
}
}
은 IntentServ입니다. Manifest – zombie
에 언급 된 패키지와 동일한 패키지에 얼음이 들어 있습니다. – Kazmi