android studio service doesn't work when i clean my app

徘徊边缘 提交于 2020-12-15 01:43:12

问题


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

This is my code

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;
    }
}

回答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



来源:https://stackoverflow.com/questions/64869193/android-studio-service-doesnt-work-when-i-clean-my-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!