アプリインスタンスも強制終了する場合は、アプリをバックグラウンドで実行したいと思います。しかし、アプリを強制終了すると、サービスも機能しなくなります。これが私のコードです。誰かが私の問題を解決するのを手伝ってください。
このリンクをたどってバックグラウンドで実行しましたが、インスタンスを削除すると機能しません。インスタンスも削除された場合、バックグラウンドサービスを実行する方法を教えてもらえますか?
これは私のMainActivityです
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ALARM_REQUEST_CODE, alarmIntent, 0);
mSensorService = new SensorService(getCtx());
mServiceIntent = new Intent(getCtx(), mSensorService.getClass());
if (!isMyServiceRunning(mSensorService.getClass())) {
startService(mServiceIntent);
}
}
これは私のサービスクラスです
public class SensorService extends Service{
public int counter=0;
public SensorService(Context applicationContext) {
super();
Log.i("HERE", "here I am!");
}
public SensorService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent broadcastIntent = new Intent("uk.ac.shef.Oak.ActivityRecognition.RestartSensor");
sendBroadcast(broadcastIntent);
}
private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, to wake up every 1 second
timer.schedule(timerTask, 1000, 1000); //
}
/**
* it sets the timer to print the counter every x seconds
*/
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
Log.i("in timer", "in timer ++++ "+ (counter++));
}
};
}
/**
* not needed
*/
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
それは長い話です。私はそれを経験しました。まだそれを実装しました。これで、私のサービスはすべてのboot_completeイベントで実行され、常に実行され続けます(通知付き)。
ビッグNO。 Google Android開発者向けドキュメントは貧弱で、適切なサンプル例もありません。これは理論的であり、理論的なものです。興味があれば読み続けてください。
https://developer.Android.com/about/versions/oreo/background
概要1:従来の受信機では、BOOT_COMPLETE
のみを受信でき、ブロードキャストはごくわずかです。常に実行されるサービスからのコードを介してそれらを登録することにより、サービスにランタイムを実装するために必要なすべてのブロードキャストレシーバーを休ませます。
概要2:繰り返しになりますが、8.0(Oreo)以降で常に実行中のプロセスを持つことはできません...常に実行中のプロセスを実現するには...作成Intentservice
タイプongoing
の適切な通知を使用して、OnStartCommand
START_STICKY
を作成し、受信者をOnCreate
のコードで登録します
実装方法:ここから参照して実装しました: Oreo:放送受信機が機能していません
今あなたの質問:アプリインスタンスも強制終了する場合は、アプリをバックグラウンドで実行したいと思います。
私自身の上記の実装リンクの助けを借りて、あなたはそれを達成することができます
*規約と条件
デバイスには、適切なAndroidオペレーティングシステムがリリースされ、そのまま書き込まれている必要があります。
はい、使用していますAndroid:
No... You are Using Funtouch OS : VIVO ( By modifying Android)
市場には多くのデバイスがありますCOLOROS:OPPO(Androidを変更することにより)......。
WhatsApp
、Facebook
、Google
Twitter
Instagram
のようにto allow only selective applications run in background
に多くの変更を加えています開発者に質問しますこれらのアプリがバックグラウンドで実行されている場合は、アプリをバックグラウンドで実行することもできます。
いいえ...これらは、サービスが許可されたベンダーからのものであるかどうかを確認するためのOSベースの変更であり、バックグラウンドで存続できるのはそのサービスのみです。これらのベンダーを許可しない場合、これらの有名なソーシャルアプリを実行しない電話を使用する人は誰もいません。
Hushhhhhhhh.....。
新しいコンセプトPIP(ピクチャーインピクチャーモード)とそれはチャネルとそれらに優先順位を作ることによってカテゴリサービス制御を持っています。あなたは通知とサービスを作成するためにオレオのためだけにコードを変更する必要があります
ここでグーグル開発者のドキュメントについて注意深く読んでください https://developer.Android.com/guide/topics/ui/notifiers/notifications 両方Javaそしてkotlinコードはここで利用可能ですoreoで通知を作成するには
https://developer.Android.com/training/notify-user/build-notification
検索してあなたと共有した後、解決策を見つけることが私の努力でした。
ここにいくつかのサンプルコードがあります:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
チャネルを作成するには、次のコードを記述します。
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
上記のリンクをクリックすると、プッシュ通知とメッセージ送信の完全な詳細を確認できます。
次のように、アプリが強制終了されたときに処理を続行するには、ForegroundServiceを作成する必要があります。
_ public class SensorService extends Service{
private PowerManager.WakeLock wakeLock;
@Override
public void onCreate() {
super.onCreate();
//wake lock is need to keep timer alive when device goes to sleep mode
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK_TAG");
createNotificationChannel(this);
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL").setSmallIcon
(<icon>).setContentTitle("Title")
.setContentText("Content").build();
startForeground(1001, notification);
}
@Override
public void onDestroy() {
super.onDestroy();
if (wakeLock.isHeld()) {
wakeLock.release();
}
}
public void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Channel name";
String description = "Description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("NOTIFICATION_CHANNEL", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
_
サービスを開始するには:
_Intent i = new Intent(context, SensorService.class);
ContextCompat.startForegroundService(context, i)
_
注:
stopSelf()
を呼び出す必要があります。