我正在开发自定义来电屏幕,但是我的问题是,当应用程序处于前台时,在来电时,广播接收器会被调用并显示自定义屏幕,但是当应用程序处于后台或未处于运行状态时,广播接收机没有被呼叫。
该如何解决?
Manifest.xml
<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);
}
}
}
}
AcceptCall.java:
目前这堂课没有什么特别的
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);
}
}
答案 0 :(得分:0)
/**
* 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;
}