2017-01-27 12 views
0

Мой фон IntentService запускается отлично после каждых 1 минуты на эмуляторе, но он не работает вообще на самом устройстве, может ли кто-нибудь сказать мне, почему? Телефон, который я использую, - это Android Infinix.IntentService работает на эмуляторе, но не на реальном устройстве

В OnCreate метод моего MainActivity, я есть:

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 лед в том же пакете, что и упомянутый в манифесте – zombie

+0

да, его в том же пакете. – Kazmi

ответ

0

попробуйте изменить закрывающий тег сюда м

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

в

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

для некоторых устройств под управлением Android 4.3 requestCode должен не быть 0

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

также может понадобиться добавить stopSelf(); к концу onHandleIntent()

+0

мой код работает ТОЛЬКО на устройстве Samsung и не работает на устройствах Infinix, Huawei и QMobile. что может быть причиной: S? – Kazmi

+0

все мобильные телефоны (кроме Infinix) запускают намерение только в том случае, если некоторые из них занимают гораздо больше времени, чем ожидалось. – Kazmi

 Смежные вопросы

  • Нет связанных вопросов^_^