当我清理我的应用程序时,android studio 服务不起作用 [英] android studio service doesn't work when i clean my app
问题描述
当有人收到消息时,我使用服务来推送通知,当我打开应用程序和关闭应用程序时,我的服务运行良好,但是当我清理我的应用程序时,它就停止了.是我自己编写的 startForeground 的问题吗而不是使用他们给的 startForeground?一开始是可行的,但是当我添加 startMyOwnForeground 和 Runnable 时,它不起作用.Runnable 是每 2 秒连接到我的数据库以检查是否有新消息,它会向用户推送通知
I use service to push notification when someone receive message,and my service works well when i open the app and close the app,but when i clean my app,it just stop.Is it the problem that i write my own startForeground instead of using the startForeground they gave?it was work at the beginning,but when i add startMyOwnForeground and Runnable,it doesn't work. Runnable is to connect to my datebase every 2 seconds to check if there is new message,and it will push notification to the user
这是我的代码
public class MyService extends Service implements getcheckfriend {
private static final int NOTIF_ID = 1;
final Handler handler = new Handler();
private static final String NOTIF_CHANNEL_ID = "Channel_Id";
public String user;
public String type = "bgnotify";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
// do your jobs here
Bundle extras = intent.getExtras();
user = (String) extras.get("user");
update(user);
System.out.println("123465897");
Runnable runnable = new Runnable() {
@Override
public void run() {
update(user);
handler.postDelayed(this, 2000);
}
};
handler.postDelayed(runnable, 2000);
return super.onStartCommand(intent, flags, startId);
}
private void startMyOwnForeground(List<Notify> result) {
if (!CollectionUtils.isEmpty(result)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
CharSequence name = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name,
importance);
Notification newMessageNotification1 =
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_baseline_comment_24)
.setContentTitle("VoCo")
.setContentText(result.get(1).getname()+"")
.setGroup("request")
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(random(), newMessageNotification1);
}
}
}
public void update(String user){
Notifybackground notifybackground = new Notifybackground(this,this);
notifybackground.execute(type,user);
}
@Override
public void checkfriendresult(List<Notify> result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground(result);
else
startForeground(1, new Notification());
}
public int random(){
Random ran = new Random();
return ran.nextInt(9999)+1;
}
}
推荐答案
有时还取决于您的移动设备.如今,移动设备的电池寿命更长.因此某些应用程序不会自动启动(或在专门打开之前接收通知).为了解决这个问题,您可以在电池设置中免除您的应用程序,例如不要优化此应用程序的使用.我个人在一加设备和小米设备中遇到过这样的问题.您还可以在此处
Sometimes it also depends on you mobile device. Nowadays mobile devices are built to last more in terms of battery life. hence some apps wont start automatically(or receive notifications until opened specifically). To overcome this issue you can exempt your app in battery settings like Don't optimize usage of this app. I have personlly faced such issue in Oneplus Device and Xiaomi device. You can also get more info regarding Mi devices here
这篇关于当我清理我的应用程序时,android studio 服务不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!