2016-09-19 54 views
1

我有實現報警經理到我的應用程序,但困惑,因爲我的告警管理器將無法啓動時,我的應用程序關閉/去世 我在那裏搜索在谷歌的許多建議,但沒有一個建議工作。這 我secenario報警管理器將無法啓動應用程序時關閉

  1. 開放的應用程序 - >自動啓動服務/鬧鐘經理
  2. 當應用程序中打開每10分鐘檢查應用服務器下載數據並插入到數據庫
  3. 時,每10分鐘一班應用程序關閉應用程序檢查到服務器下載數據並插入到數據庫中

問題是當應用程序關閉時服務也停止。 此我的示例代碼

MainActivity.java

AlarmReceiver alarm = new AlarmReceiver(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    alarm.setAlarm(this); 
} 

AlarmReceiver.Java

public class AlarmReceiver extends WakefulBroadcastReceiver { 
private AlarmManager alarmMgr; 
private PendingIntent alarmIntent; 

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

    Intent service = new Intent(context, SchedulingService.class); 
    startWakefulService(context, service); 
} 

public void setAlarm(Context context) { 
    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, AlarmReceiver.class); 
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 


    alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       10000, 
       10000, alarmIntent); 

    ComponentName receiver = new ComponentName(context, SampleBootReceiver.class); 
    PackageManager pm = context.getPackageManager(); 

    pm.setComponentEnabledSetting(receiver, 
      PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
      PackageManager.DONT_KILL_APP);   
} 

public void cancelAlarm(Context context) { 
    // If the alarm has been set, cancel it. 
    if (alarmMgr!= null) { 
     alarmMgr.cancel(alarmIntent); 
    } 


    ComponentName receiver = new ComponentName(context, BootReceiver.class); 
    PackageManager pm = context.getPackageManager(); 

    pm.setComponentEnabledSetting(receiver, 
      PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
      PackageManager.DONT_KILL_APP); 
} } 

BootReceiver.java

public class BootReceiver extends BroadcastReceiver { 
AlarmReceiver alarm = new AlarmReceiver(); 
@Override 
public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
    { 
     alarm.setAlarm(context); 
    } 
}} 

ScheduleService.java

public class SchedulingService extends IntentService { 
public SchedulingService() { 
    super("SchedulingService"); 
} 

public static final String TAG = "Scheduling Demo"; 
public static final int NOTIFICATION_ID = 1; 
public static final String SEARCH_STRING = "Active"; 
public static final String URL = "http://localhost/TMALive"; 
private NotificationManager mNotificationManager; 
NotificationCompat.Builder builder; 

@Override 
protected void onHandleIntent(Intent intent) { 
    String urlString = URL; 
    String result =""; 
    try { 
     result = loadFromNetwork(urlString); 
    } catch (IOException e) { 
     Log.i(TAG, getString(R.string.connection_error)); 
    } 

    if (result.indexOf(SEARCH_STRING) != -1) { 
     sendNotification(getString(R.string.live_found)); 
     Log.i(TAG, "Your Post Live!!"); 
    } else { 
     sendNotification(getString(R.string.no_live)); 
     Log.i(TAG, "Your Post Off. :-("); 
    } 
    AlarmReceiver.completeWakefulIntent(intent); 

} 


private void sendNotification(String msg) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
     new Intent(this, MainActivity.class), 0); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContentTitle(getString(R.string.pos_alert)) 
    .setStyle(new NotificationCompat.BigTextStyle() 
    .bigText(msg)) 
    .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 
private String loadFromNetwork(String urlString) throws IOException { 
    InputStream stream = null; 
    String str =""; 

    try { 
     stream = downloadUrl(urlString); 
     str = readIt(stream); 
    } finally { 
     if (stream != null) { 
      stream.close(); 
     }  
    } 
    return str; 
} 


private InputStream downloadUrl(String urlString) throws IOException { 

    URL url = new URL(urlString); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setReadTimeout(10000 /* milliseconds */); 
    conn.setConnectTimeout(15000 /* milliseconds */); 
    conn.setRequestMethod("GET"); 
    conn.setDoInput(true); 
    // Start the query 
    conn.connect(); 
    InputStream stream = conn.getInputStream(); 
    return stream; 
} 


private String readIt(InputStream stream) throws IOException { 

    StringBuilder builder = new StringBuilder(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); 
    for(String line = reader.readLine(); line != null; line = reader.readLine()) 
     builder.append(line); 
    reader.close(); 
    return builder.toString(); 
}} 

這就是代碼工作正常,當應用程序打開,但是當應用程序關閉顯示,並重新啓動後也沒有通知。

你能給我建議女巫更好的使用這種方法或使用服務的方法呢?

千恩萬謝

+0

那麼,我做了什麼文檔在這裏說:https://developer.android.com/training/scheduling/alarms.html。到目前爲止,它工作得很好。即使手機重新啓動或應用程序未運行。 –

+0

一些魅族手機具有自能源經理電話時關閉屏幕 – Beyka

+0

這就是意味着更好的使用服務,可以破壞你的報警?但是如果使用服務如何每10分鐘創建一次服務數據? – Kenjin

回答

0

有同樣的問題,在我的情況下,它竟然是因爲所謂的「自動啓動管理器」的設備上的股票應用程序(華碩手機),我是測試上,這可以防止應用程序在後臺啓動,除非用戶明確允許它。 (不提供任何警告)

雖然這是一個渺茫的機會,但如果您根據本書完成了所有操作並且在應用程序運行時仍然無法運行,您可能需要檢查系統上是否運行了類似的軟件。

0

每當關閉應用程序或從最近的應用中移除,它調用onTrimMemory()方法。

你又需要一套報警管理器時,應用程序關閉或從最近的應用中移除。 覆蓋延伸Application的類中的以下方法。

@Override 
public void onTrimMemory(int level) { 
    super.onTrimMemory(level); 
    // set alarm here 
}