小编典典

如何使该电话状态始终保持广播接收器正常工作?(不间断)

java

我正在创建一个应用程序,它将在呼叫状态之前和之后显示吐司消息。我在手机上生成并安装了应用程序。 几天甚至几个小时后,它运行良好。
但是之后,它停止了,为了运行该应用程序,我需要再次打开该应用程序。如何修改我的应用程序以使其运行良好?谢谢

表现

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.INTERNET" />


    <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"></activity>
        <activity android:name=".MainActivity2">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> <!-- This part is inside the application -->
        <receiver
            android:name=".CallReceiver"
            android:enabled="true">
            <intent-filter android:priority="999">
                <action android:name="android.intent.action.PHONE_STATE" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

MainActivity.class

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

       checkAndRequestPermissions();

      }
    private  boolean checkAndRequestPermissions() {
 int readPhoneState = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
 int read_call_log = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG);

 List listPermissionsNeeded = new ArrayList<>();

 if (readPhoneState != PackageManager.PERMISSION_GRANTED) {
     listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
 }

 if (read_call_log != PackageManager.PERMISSION_GRANTED) {
     listPermissionsNeeded.add(Manifest.permission.READ_CALL_LOG);
 }

 if (read_call_log != PackageManager.PERMISSION_GRANTED) {
     listPermissionsNeeded.add(Manifest.permission.PROCESS_OUTGOING_CALLS);
 }

 if (read_call_log != PackageManager.PERMISSION_GRANTED) {
     listPermissionsNeeded.add(Manifest.permission.INTERNET);
 }

 if (!listPermissionsNeeded.isEmpty()) {
     ActivityCompat.requestPermissions(this,
             (String[]) listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
             REQUEST_ID_MULTIPLE_PERMISSIONS);

     return false;
 }
 return true;
}

CallReciver.class

public class CallReceiver extends BroadcastReceiver{


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

            try {

                runfirstTime(context,intent);
                Toast.makeText(context, "1st", Toast.LENGTH_SHORT).show();

            } catch (Exception ex) {
                try {

                }
                catch (Exception e)
                {

                }
            }
        }
      }

阅读 193

收藏
2020-11-26

共1个答案

小编典典

除非创建前台服务,否则新版本的Android不允许您执行您要实现的目标。

Android不想在没有用户明确知识的情况下在后台运行应用程序。

因此,您需要创建一个前台服务,并在通知面板中显示持久性通知。然后,您可以从前景服务中创建一个BroadcastReceiver。

使用以下代码将您的服务作为前台服务启动

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   context.startForegroundService(intent);
}
else {
   context.startService(intent); 
}

并在服务中onCreate()做这样的事情:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   startForeground(1, new Notification());
}

下面的链接将使您对前景服务有更多的了解。

https://blog.logimonk.com/post/foreground-service-in-
android

https://medium.com/@debuggingisfun/android-o-work-around-background-service-
limitation-e697b2192bc3

从服务创建您的BroadcastReceiver。

2020-11-26