Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 来电时,当应用程序处于后台或应用程序未运行时,不会调用广播接收器_Android_Android Service_Android Broadcastreceiver - Fatal编程技术网

Android 来电时,当应用程序处于后台或应用程序未运行时,不会调用广播接收器

Android 来电时,当应用程序处于后台或应用程序未运行时,不会调用广播接收器,android,android-service,android-broadcastreceiver,Android,Android Service,Android Broadcastreceiver,我正在开发一个自定义来电屏幕,但我的问题是,当应用程序位于前台时,在来电时会调用广播接收器并显示自定义屏幕,但当应用程序位于后台或未处于运行状态时,则不会调用广播接收器。 如何解决这个问题 Manifest.xml AcceptCall.java: 目前这门课没有什么特别的 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:

我正在开发一个自定义来电屏幕,但我的问题是,当应用程序位于前台时,在来电时会调用广播接收器并显示自定义屏幕,但当应用程序位于后台或未处于运行状态时,则不会调用广播接收器。 如何解决这个问题

Manifest.xml

AcceptCall.java: 目前这门课没有什么特别的

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <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">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AcceptCall"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.ANSWER" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".broadcast.PhoneListenerBroad">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>
public class PhoneListenerBroad extends BroadcastReceiver {

    Context c;
    private String outgoing;

    @Override
    public void onReceive(Context context, Intent intent) {
        c = context;

        Log.d("arsalan","Broadcast receiver is called");

        try {
            TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
            tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }

    }

    private class MyPhoneStateListener extends PhoneStateListener {
        public void onCallStateChanged(final int state, final String incomingNumber) {

            if (state == TelephonyManager.CALL_STATE_RINGING) {
                Intent intentPhoneCall = new Intent(c, AcceptCall.class);
                intentPhoneCall.putExtra("incomingnumber", incomingNumber);
                intentPhoneCall.putExtra("state", state);
                intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                c.startActivity(intentPhoneCall);
                Log.d("arsalan","incoming call: "+incomingNumber);
            }
        }
    }
}
public class AcceptCall extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accept_call);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    }
}
/**
 * Method checks if the app is in background or not
 *
 * @param context Application context
 * @return True/False
 */
public static boolean isAppIsInBackground(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        isInBackground = false;
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            isInBackground = false;
        }
    }

    return isInBackground;
}