2017-01-27 45 views
0

私の背景IntentServiceは、エミュレータで1分ごとに完全に起動しますが、実際のデバイスではまったく動作していません。 私が使用している電話はAndroid Infinixです。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

があちこちに終了タグを変更してみてください私は持っている私のMainActivityののonCreateメソッドで

メートル

requestCodeonHandleIntent()

の終わりに stopSelf();を追加する必要があるかもしれませんも0

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

すべきではないアンドロイド4.3を実行しているいくつかのデバイスのための

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

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

+0

私のコードは「Samsung device」のみで実行され、「Infinix、Huawei、QMobileデバイス」では実行されません。理由は何か:S? – Kazmi

+0

すべての携帯電話(Infinixを除く)は、その一部が予想よりも長くかかるという意図のみを引き起こしています。 – Kazmi

関連する問題