2017-01-27 3 views
0

내 백그라운드 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(); 
    } 
} 
} 
+0

은 IntentServ입니다. Manifest – zombie

+0

에 언급 된 패키지와 동일한 패키지에 얼음이 들어 있습니다. – Kazmi

답변

0

것은 이리저리 종료 태그를 변경해보십시오 m

<service 
     android:name=".RemoveDownloadService" 
     android:exported="false"/> 

일부 장치에 대한

<service 
     android:name=".RemoveDownloadService" 
     android:exported="false"> 
</service> 

안드로이드 실행에 4.3 requestCode하지해야한다 0

int requestCode = 1; 
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

또한 onHandleIntent()

의 끝에 stopSelf();를 추가해야 할 수도 있습니다
+0

내 코드는 'Samsung device'에서만 실행되고 'Infinix, Huawei 및 QMobile devices'에서는 실행되지 않습니다. 그 이유는 무엇일까요? – Kazmi

+0

모든 모바일 (Infinix 제외)은 의도 된 것 중 일부가 예상보다 오래 걸리는 경우에만 트리거합니다. – Kazmi

관련 문제