Androidでサービスを使用する方法を学習しています。私はそれらを理解するためのアプリを開発しています。ユーザーが「開始」ボタンを押すと、サービスが開始されます。サービスはAndroidManifestで「停止」ボタン(Alpifyまたは類似のようなアプリ)ユーザープレスまで私のサービスがシステムによってなぜ殺されるのですか?
を実行されますが、私のサービスは、以下のように宣言されます。Activityから
<service
android:name="com.cpalosrejano.example.MyService"
android:stopWithTask="false"
android:enabled="true" />
、私はとしてサービスを開始従い
Intent service = new Intent(MyActivity.this, MyService.class);
startService(service);
それから私は、他のアプリは、RAMを大量に消費する(などロワイヤル、Instagramの、Facebookを利用して、激突...)と私のサービスは、システムによって殺されるWICH開きます。
私はonTrimMemory(int level)
メソッドを実装して何が起こっているかを確認しました。
onTrimMemory() : 5
onTrimMemory() : 10
onTrimMemory() : 15
私はonTrimMemory()
メソッドの動作をreadedている:私のサービスが殺されている前に、logcatは私にフォローの情報を提供します。ドキュメンテーションには、コードを受け取ったときに、未使用のオブジェクトをリリースする必要があります。 しかし私のサービスにはまだコードがありません。
:wakelock
コードを取得サービス
startForeground()
START_STICKY
フラグでlargeHeap="true"
を設定
- 私のサービス:
public class MyService extends Service { PowerManager.WakeLock mWakeLock; @Override public void onCreate() { Log.i(ServiceBase.class.getSimpleName(), "onCreate() : Service Started."); super.onCreate(); } @Override public final int onStartCommand(Intent intent, int flags, int startId) { Log.i(ServiceBase.class.getSimpleName(), "onStarCommand() : Received id " + startId + ": " + intent); // acquire wakelock PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ServiceBase.class.getSimpleName()); mWakeLock.acquire(); // start foreground NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(android.R.drawable.stat_sys_download); builder.setContentText("Service in foreground"); builder.setContentTitle("My app"); builder.setOngoing(true); Notification notification = builder.build(); startForeground(1359, notification); // run until explicitly stopped. return START_STICKY; } @Nullable @Override public IBinder onBind(Intent intent) { Log.i(ServiceBase.class.getSimpleName(), "onBind() : true"); return null; } @Override public void onRebind(Intent intent) { Log.i(ServiceBase.class.getSimpleName(), "onRebind() : true"); super.onRebind(intent); } @Override public boolean onUnbind(Intent intent) { Log.i(ServiceBase.class.getSimpleName(), "onUnbind() : false"); return super.onUnbind(intent); } @Override public void onDestroy() { Log.i(ServiceBase.class.getSimpleName(), "onDestroy()"); super.onDestroy(); } @Override public void onTaskRemoved(Intent rootIntent) { Log.i(ServiceBase.class.getSimpleName(), "onTaskRemoved()"); super.onTaskRemoved(rootIntent); } @Override public void onTrimMemory(int level) { Log.i(ServiceBase.class.getSimpleName(), "onTrimMemory() : " + level); super.onTrimMemory(level); } }
そして今、私の質問:私のアプリは、システムによって殺されている間に可能である方法
、のRuntasticアプリが実行されていますか?
ありがとうございました!私はその記事を読んだことはありませんでした。便利でした。だから、あなたがGPSの場所を取得するサービスが開始された場合、デバイスが低メモリで動作しない限り、Androidはそれを殺さないだろうと思いますか? – cpalosrejano