2016-09-19 16 views
1

アプリケーションにアラームマネージャを実装していますが、アプリケーションが終了したときにアラームマネージャが起動しないために混乱しています Googleで多数の提案を探していました作業。 これは私のsecenarioアプリケーションが終了するとアラームマネージャが起動しない

  1. オープン用途向け>自動起動サービス/アラームマネージャ
  2. アプリケーションが
  3. 時に近いアプリケーションを10分ごとにデータをダウンロードし、データベースに挿入するために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

いくつかのmeizuの携帯電話は、電話が画面をオフにするとあなたのアラームを破壊することができます自己エネルギーマネージャーを持っています – Beyka

+0

thatsは良いサービスを使用していることを意味ですか?サービスを使用する場合どのように10分ごとにサービスtrigerデータを作るには? – Kenjin

答えて

0

は同じ問題を抱えていたし、私の場合にはそれがために私がテストしていたデバイス上で「自動起動マネージャ」と呼ばれる株式のアプリ(ASUS電話)であることが判明しましたユーザーが明示的に許可しない限り、アプリはバックグラウンドで開始されません。

これはスリムなチャンスですが、書籍にしたがってすべてを行い、アプリが動作しているときにはまだ動作していない場合、類似のソフトウェアがシステムで実行されているかどうかを確認したい場合があります。

0

最近のアプリからアプリを閉じるか削除するたびに、onTrimMemory()メソッドが呼び出されます。

最近のアプリからアプリケーションを終了または削除する場合は、再度アラームマネージャを設定する必要があります。 Applicationを拡張するクラスの中で、次のメソッドをオーバーライドします。

@Override 
public void onTrimMemory(int level) { 
    super.onTrimMemory(level); 
    // set alarm here 
} 
関連する問題