当我清理我的应用程序时,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;
}
}
推荐答案
有时它还取决于您的移动设备.如今,移动设备的续航时间更长.因此,某些应用不会自动启动(或收到通知,直到专门打开).为解决此问题,您可以在电池设置中免除您的应用程序,例如不要优化此应用程序的使用".我曾在Oneplus设备和小米设备中亲自遇到过此类问题.您还可以在此处
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屋!