2015-10-06 83 views
0

我想爲我的學校提出申請,其中包括一個後臺服務,將下載學生的成績和作業歷史記錄。該服務每小時由AlarmManager調用,並且只有在設備開機時纔有效。一旦設備打開(從休眠狀態開始;設備未完全關閉),AlarmManager觸發該服務。這是廣播接收器,其中包括AlarmManager的代碼:Android - 後臺服務不運行,如果手機睡着

import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.support.v4.content.WakefulBroadcastReceiver; 

public class ServiceAlarmReceiver extends WakefulBroadcastReceiver {  

private final String TAG = "com.example.ahsandroidapplication"; 

@Override 
public void onReceive(Context context, Intent intent) { 

    Log.i(TAG, "Broadcast received"); 
    Intent gradeNotificationServiceIntent = new Intent(context, GradeNotificationService.class); 
    //context.startService(gradeNotificationServiceIntent); 
    startWakefulService(context, gradeNotificationServiceIntent); 
} 

public static void setAlarm(Context context){ 

    System.out.println("Service started"); 

    long alertTime = System.currentTimeMillis() + 1000 * 60 * 60; 
    Intent alertIntent = new Intent(context, ServiceAlarmReceiver.class); 

    alertIntent.setAction("com.example.ahsandroidapplication.servicealarmbroadcast"); 

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    //alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(context, 1, alertIntent, 
    //  PendingIntent.FLAG_UPDATE_CURRENT)); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alertTime, 1000 * 3600, PendingIntent.getBroadcast(context, 
      GradeNotificationService.SERVICE_ALARM_BROADCAST_ID, alertIntent, 
      PendingIntent.FLAG_UPDATE_CURRENT)); 


} 

public static void cancelAlarm(Context context) 
{ 
    Intent intent = new Intent(context, ServiceAlarmReceiver.class); 

    intent.setAction("com.example.ahsandroidapplication.servicealarmbroadcast"); 

    PendingIntent sender = PendingIntent.getBroadcast(context, 
      GradeNotificationService.SERVICE_ALARM_BROADCAST_ID, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.cancel(sender); 
} 
} 

因爲廣播接收,只有當手機處於開機狀態,我相信問題出在接收器。

該服務還存在另一個問題。無論何時發佈新成績,都應該發送推送通知,但他們從未被髮送。

下面是服務類的代碼:

import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 

import android.app.IntentService; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.support.v4.app.NotificationCompat; 

public class GradeNotificationService extends IntentService { 

private Context context; 

public GradeNotificationService() { 
    super("GradeNotificationService"); 
    // TODO Auto-generated constructor stub 
} 

public static final int SERVICE_ALARM_BROADCAST_ID = 0; 

@Override 
protected void onHandleIntent(Intent intent) { 
    // TODO Auto-generated method stub 

    System.out.println("Grade notification services started"); 
    context = getApplicationContext(); 
    final Intent completeWakefulIntent = intent; 

    if(gradesChanged()){ 

     getAssignments(); 
    } 

    ServiceAlarmReceiver.completeWakefulIntent(completeWakefulIntent); 
} 


private boolean gradesChanged() { 

//Massive blocks of code that will take you forever to read 

} 

private void getAssignments(){ 


//Massive blocks of code that will take you forever to read 
//It is in this method that sendNotification() is used to send a notification 
//(at least that's what it's supposed to be doing) 


} 

private void sendNotification(String title, String message){ 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 

    mBuilder.setSmallIcon(R.drawable.ic_launcher); 
    mBuilder.setContentTitle(title); 
    mBuilder.setContentText(message); 
    mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND); 
    mBuilder.setAutoCancel(true); 

    Intent resultIntent = new Intent(context, MainInterface.class); 

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0); 
    mBuilder.setContentIntent(resultPendingIntent); 

    NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    // mId allows you to update the notification later on. 
    mNotificationManager.notify(1, mBuilder.build()); 

} 

private boolean networkIsAvailable() { 

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    // return netInfo != null && netInfo.isConnectedOrConnecting(); 

    NetworkInfo mWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    return netInfo != null && netInfo.isConnectedOrConnecting() 
      || mWifi.isConnected(); 

} 

} 

誰能告訴我,我能做些什麼,使這個服務運行的時候,手機是睡着了,或者爲什麼它不發送任何通知?任何幫助appriciated。

編輯:

這是大部分清單。目標SDK是21,最小爲11:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 

<uses-sdk 
    android:minSdkVersion="11" 
    android:targetSdkVersion="21" /> 

<application 
    android:allowBackup="true" 
    android:label="@string/app_name" 
    android:icon="@drawable/ic_launcher"> 

    <service 
     android:name="com.example.ahsandroidapplication.GradeNotificationService" 
     android:enabled="true" > 
     <intent-filter> 
      <action android:name="com.example.ahsandroidapplication.ServiceAlarmReceiver" /> 
     </intent-filter> 
    </service> 

    <receiver android:name=".ServiceAlarmReceiver" 
     android:enabled="true"> 
     <intent-filter> 
     <action android:name="com.example.ahsandroidapplication.servicealarmbroadcast"></action> 
     </intent-filter> 
    </receiver> 

    <receiver android:name=".BootReceiver" 
    android:enabled="true"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"></action> 
     </intent-filter> 
    </receiver> 

    <activity 
     android:name="MainInterface" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name="MainLogin" 
       android:label="@string/app_name"> 

    </activity> 

    <service android:enabled="true" 
     android:exported="false" 
     android:isolatedProcess="false" 
     android:label="@string/app_name" 
     android:name="AspenManager" > 
    </service> 

</application> 
+0

請發表您的清單對於'ServiceAlarmReceiver'了''元素。另外,你測試的是哪個版本的Android,以及你的'targetSdkVersion'是什麼? – CommonsWare

回答

0

「注:與API開始19(奇巧)報警交付是不精確的:操作系統將爲了儘量減少喚醒和電池使用Shift報警有新API支持需要嚴格交付保證的應用程序;請參閱setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。targetSdkVersion早於API 19的應用程序將繼續看到之前的行爲,準確地在需要時提供。「

http://developer.android.com/reference/android/app/AlarmManager.html