当应用程序关闭并再次启动时,后台服务计数循环 运行 以其双倍速度
When app close and start again background service count loop run with its double speed
我在后台服务 class 中实现了一个简单的计数程序,它在一个 second.it 之后递增变量,并且在使用 intent 更新服务数据后在主 activity 中显示数据。但主要问题是,当我关闭应用程序时,它仍然在后台 运行 但当我再次启动它时,计数速度增加意味着变量值在 1 秒内更改 2 3 次。如果我在大约 1 分钟后启动应用程序,则价值速度会快 10 到 20 倍。不知道是程序出了什么问题
我的服务Class
package com.darkcoderz.backgroundservices;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.JobIntentService;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import java.util.logging.Handler;
public class BgService extends Service {
int i=0;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showNotification();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while(i<1000)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
sendData(i);
}
stopForeground(true);
stopSelf();
}
});
thread.start();
return START_STICKY;
}
private void sendData(int idata) {
Intent intent = new Intent("donorrams");
intent.putExtra("hello",idata);
sendBroadcast(intent);
}
private void showNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel("donation","donation", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
Intent result = new Intent(this,BgService.class);
PendingIntent pendresult = PendingIntent.getActivity(this,1,result,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"donation")
.setContentTitle("DonorRams")
.setAutoCancel(true)
.setContentText("We start processing on your donation")
.setContentIntent(pendresult);
Notification manager = builder.build();
//manager.notify(999,builder.build());
startForeground(123, manager);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
主要活动
package com.darkcoderz.backgroundservices;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent service = new Intent(MainActivity.this,BgService.class);
startService(service);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter(
"donorrams");
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//extract our message from intent
TextView count = findViewById(R.id.count);
int msg_for_me = intent.getIntExtra("hello",0);
//Toast.makeText(context, ""+msg_for_me, Toast.LENGTH_SHORT).show();
count.setText(""+msg_for_me);
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//unregister our receiver
this.unregisterReceiver(this.mReceiver);
}
}
private Thread thread;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
if (thread == null) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
setThread(null);
}
});
setThread(t);
t.start();
}
...
}
private synchronized void setThread(Thread thread) {
this.thread = thread;
}
我在后台服务 class 中实现了一个简单的计数程序,它在一个 second.it 之后递增变量,并且在使用 intent 更新服务数据后在主 activity 中显示数据。但主要问题是,当我关闭应用程序时,它仍然在后台 运行 但当我再次启动它时,计数速度增加意味着变量值在 1 秒内更改 2 3 次。如果我在大约 1 分钟后启动应用程序,则价值速度会快 10 到 20 倍。不知道是程序出了什么问题
我的服务Class
package com.darkcoderz.backgroundservices;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.JobIntentService;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import java.util.logging.Handler;
public class BgService extends Service {
int i=0;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showNotification();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while(i<1000)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
sendData(i);
}
stopForeground(true);
stopSelf();
}
});
thread.start();
return START_STICKY;
}
private void sendData(int idata) {
Intent intent = new Intent("donorrams");
intent.putExtra("hello",idata);
sendBroadcast(intent);
}
private void showNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel("donation","donation", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
Intent result = new Intent(this,BgService.class);
PendingIntent pendresult = PendingIntent.getActivity(this,1,result,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"donation")
.setContentTitle("DonorRams")
.setAutoCancel(true)
.setContentText("We start processing on your donation")
.setContentIntent(pendresult);
Notification manager = builder.build();
//manager.notify(999,builder.build());
startForeground(123, manager);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
主要活动
package com.darkcoderz.backgroundservices;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver mReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent service = new Intent(MainActivity.this,BgService.class);
startService(service);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter(
"donorrams");
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//extract our message from intent
TextView count = findViewById(R.id.count);
int msg_for_me = intent.getIntExtra("hello",0);
//Toast.makeText(context, ""+msg_for_me, Toast.LENGTH_SHORT).show();
count.setText(""+msg_for_me);
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//unregister our receiver
this.unregisterReceiver(this.mReceiver);
}
}
private Thread thread;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
if (thread == null) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
setThread(null);
}
});
setThread(t);
t.start();
}
...
}
private synchronized void setThread(Thread thread) {
this.thread = thread;
}