2017-05-17 1 views
2

앱이 스택에서 삭제 되더라도 백그라운드에서 서비스를 실행하고 싶습니다. 이 기능은 일부 장치에서 정상적으로 작동합니다. 그러나 앱이 죽으면 oppo와 vivo 휴대 전화에서는 실행되지 않습니다. 이에 대한 해결책이 있습니까? 그렇지 않은 경우 허가 허용 화면을 어떻게 열 수 있습니까?안드로이드의 백그라운드 서비스가 oppo, vivo 등의 일부 장치를 실행하지 않습니다.

Timer_Service.java 사전에

import android.app.Service; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Handler; 
import android.os.IBinder; 
import android.preference.PreferenceManager; 
import android.support.annotation.Nullable; 
import android.text.LoginFilter; 
import android.util.Log; 

import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.Random; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.concurrent.TimeUnit; 

import static com.gov.ojas1.WebView.TimerReceiver.CUSTOM_INTENT; 


public class Timer_Service extends Service { 

    public static String str_receiver = "com.countdowntimerservice.receiver"; 

    private Handler mHandler = new Handler(); 
    Calendar calendar; 
    SimpleDateFormat simpleDateFormat; 
    String strDate; 
    Date date_current, date_diff; 
    SharedPreferences mpref; 
    SharedPreferences.Editor mEditor; 

    private Timer mTimer = null; 
    public static final long NOTIFY_INTERVAL = 1000; 
    Intent intent; 
    long int_timer; 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     mpref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     mEditor = mpref.edit(); 
     calendar = Calendar.getInstance(); 
     simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); 

     mTimer = new Timer(); 
     mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL); 
     intent = new Intent(str_receiver); 

     Random r = new Random(); 
     int i1 = r.nextInt(10800000 - 7200000) + 7200000; 
//  int i1 = r.nextInt(300000 - 180000) + 180000; 
//  int_timer = (long) (int) i1; 

     int_timer =10000; 
    } 


    class TimeDisplayTimerTask extends TimerTask { 

     @Override 
     public void run() { 
      mHandler.post(new Runnable() { 

       @Override 
       public void run() { 

        calendar = Calendar.getInstance(); 
        simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); 
        strDate = simpleDateFormat.format(calendar.getTime()); 
        Log.e("strDate", strDate); 
        twoDatesBetweenTime(); 

       } 

      }); 
     } 

    } 

    public String twoDatesBetweenTime() { 


     try { 
      date_current = simpleDateFormat.parse(strDate); 
     } catch (Exception e) { 

     } 

     try { 
      date_diff = simpleDateFormat.parse(mpref.getString("data", "")); 
     } catch (Exception e) { 

     } 

     try { 


      long diff = date_current.getTime() - date_diff.getTime(); 
      Log.e("Diff", diff + ""); 


      Log.e("TImer", int_timer + ""); 


      long long_hours = int_timer - diff; 
      long diffSeconds2 = long_hours/1000 % 60; 
      long diffMinutes2 = long_hours/(60 * 1000) % 60; 
      long diffHours2 = long_hours/(60 * 60 * 1000) % 24; 


      if (long_hours > 0) { 
       String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2; 

       Log.e("TIME", str_testing); 

      } else { 
       String str_testing = diffHours2 + ":" + diffMinutes2 + ":" + diffSeconds2; 
       mEditor.putString("data", strDate).commit(); 
       fn_update(); 
       Log.e("TIME", "Finish"); 


      } 
     } catch (Exception e) { 
      mTimer.cancel(); 
      mTimer.purge(); 
      mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL); 
      e.printStackTrace(); 


     } 

     return ""; 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.e("Service finish", "Finish"); 
    } 

    private void fn_update() { 

     Intent i = new Intent(); 
     i.setAction(CUSTOM_INTENT); 
     i.putExtra("boolean", true); 
     sendBroadcast(i); 

    } 

} 

감사 :

여기 내 코드입니다.

+0

대부분의 경우 Android 휴대 전화에서 발생하는 모든 이벤트에 대해 broadCast 리시버를 만듭니다. 그리고 거기에서 당신의 서비스를 시작하십시오. –

+0

또는 알람 관리자를 대신 사용하십시오. –

+0

나는 내 질문을 업데이트하고 지금 사용하고있는 서비스 클래스를 추가했다. 사실 삼성, 레노보 등에서 잘 작동합니다.하지만이 서비스는 자동으로 oppo, vivo 및 다른 새로운 OS 장치에서 다시 시작하지 않습니다. – Dev

답변

0

서비스를 포 그라운드로 실행하면이 문제를 해결할 수 있습니다. 당신은 다음 링크를 참조 할 수 있습니다

는 동일한 작업을 수행합니다 :

<service [name, etc] 
    android:process=":externalprocess"/> 

이을 : 나 서비스 태그에 매니페스트에서 android:process 태그를 사용하고있었습니다 근무 무엇

Android - implementing startForeground for a service?

+0

이것은 특정 장치의 문제이다. 이와 같은 표준 솔루션은 문제를 해결하지 못합니다. –

+0

Oppo에서 작동하지 않습니다. ( –

0

별도의 프로세스에서 실행되므로 서비스가 앱과 함께 종료되지 않습니다.

+0

아니요 작동하지 않습니다. – Dev