服务不适用于Oreo 8.1-检测器不起作用,后台处理也不起作用

时间:2018-06-28 14:08:43

标签: java android service background android-8.0-oreo

我的呼叫检测器应用程序在除8 Oreo之外的所有android版本上均可正常运行。我得到一个死锁范例:检测器未调用,当我关闭应用程序时,系统将其杀死,而不是将其留在后台。

我已经阅读了从电缆到尾巴https://developer.android.com/about/versions/oreo/background的文档,我在google上看起来很丰富,并且这里的内容非常丰富,但是由于需要“危险”权限,因此没有适用于我的情况的解决方案

我的情况似乎很简单:我有一个检测器,一个服务和一个带按钮的主体。当用户拨打某个号码时,我希望它打开我的主电话。

解决此问题的正确方法是什么?

manifest.xml:

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <service
        android:name=".CallDetectionService"
        android:enabled="true"
        android:exported="false"/>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </activity>
</application>

CallDetectionService.java

public class CallDetectionService extends Service {

    private CallDetector callDetector;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        callDetector = new CallDetector(this);
        int r = super.onStartCommand(intent, flags, startId);
        callDetector.start();
        return r;

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        callDetector.stop();
    }



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

}

CallDetector.java

public class CallDetector {
    public static final String MY_PREF = "MY_PREF";
    public static final String NUMBER_KEY = "NUMBER_KEY";

    private SharedPreferences sharedPreferences;

    public class OutgoingDetector  extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);


            String compare_num = "12345678";
            if (number.equals(compare_num)) {
                Intent i = new Intent(context, MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                setResultData(null);
                context.startActivity(i);
            }
        }
    }

    private Context ctx;
    private OutgoingDetector outgoingDetector;

    public CallDetector(Context ctx) {
        this.ctx = ctx;
        outgoingDetector = new OutgoingDetector();
    }

    public void start() {
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
        ctx.registerReceiver(outgoingDetector, intentFilter);
    }

    public void stop(){
        ctx.unregisterReceiver(outgoingDetector);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button button;
    private TextView textView;
    private boolean detecting = false;
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 0;


    public boolean isPermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(android.Manifest.permission.CALL_PHONE)
                    == PackageManager.PERMISSION_GRANTED) {
                Log.v("TAG", "Permission is granted");
                return true;

            } else {

                Log.v("TAG", "Permission is revoked");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                return false;
            }
        } else { 
            Log.v("TAG", "Permission is granted");
            return true;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);
        /* button.setOnClickListener(this); */


        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {


            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.CALL_PHONE)) {



            } else {


                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.CALL_PHONE},
                        MY_PERMISSIONS_REQUEST_CALL_PHONE);


            }
        }

        String action = "START";
        final Intent intent = new Intent(this, CallDetectionService.class);
        intent.setAction(action);
        startService(intent);

    }




    @Override

    public void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getSharedPreferences(CallDetector.MY_PREF, MODE_PRIVATE);
        String number = sharedPreferences.getString(CallDetector.NUMBER_KEY, "URA VISUAL");
        textView.setText(number);
    }

}

我做错了什么?我一直在寻找解决方案已有4天了,但仍然找不到任何东西。

0 个答案:

没有答案