앱을 청소할 때 Android Studio 서비스가 작동하지 않습니다.

Nov 16 2020

누군가 메시지를받을 때 푸시 알림 서비스를 사용하고, 앱을 열고 앱을 닫으면 서비스가 잘 작동하지만 앱을 정리하면 그냥 멈 춥니 다. 사용하는 대신 내 자신의 startForeground를 작성하는 것이 문제입니까? 그들이 준 startForeground? 처음에는 작업 이었지만 startMyOwnForeground 및 Runnable을 추가하면 작동하지 않습니다. Runnable은 2 초마다 내 데이터베이스에 연결하여 새 메시지가 있는지 확인하고 사용자에게 알림을 푸시하는 것입니다.

이것은 내 코드입니다

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

답변

NadeemShaikh Nov 17 2020 at 06:58

때로는 모바일 장치에 따라 다릅니다. 오늘날 모바일 장치는 배터리 수명 측면에서 더 오래 지속되도록 제작되었습니다. 따라서 일부 앱은 자동으로 시작되지 않습니다 (또는 특별히 열릴 때까지 알림 수신). 이 문제를 해결하려면이 앱의 사용을 최적화하지 않음과 같은 배터리 설정에서 앱을 면제 할 수 있습니다. Oneplus Device 및 Xiaomi 장치에서 이러한 문제에 직접 직면했습니다. 여기에서 Mi 장치에 대한 자세한 정보를 얻을 수도 있습니다.