简体   繁体   English

我怎样才能让这个电话状态广播接收器一直工作?(不停)

[英]How can I make this phone call states Broadcast receiver to work all the times?(non-stops)

I'm creating an application, which will show toast messages before and after the call states.我正在创建一个应用程序,它将在通话状态之前和之后显示 toast 消息。 I generated and installed my application on my mobile.我在我的手机上生成并安装了我的应用程序。 it was working well after a few days or sometimes a few hours.几天或有时几个小时后它运行良好。 but after that, it's stopped, in order to run the application I need to open the application again.但在那之后,它停止了,为了运行应用程序,我需要再次打开应用程序。 how can I modify my application to work very well?如何修改我的应用程序以使其正常工作? Thank you谢谢

manifest显现

<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 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 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)
                {

                }
            }
        }
      }

New versions of Android does not allow you to do what you are trying to achieve, unless you create foreground service.新版本的 Android 不允许你做你想要实现的,除非你创建前台服务。

Android does not want apps to run in background without explicit knowledge of the user. Android 不希望应用程序在用户不明确知道的情况下在后台运行。

So you need to create a foreground service and show a persistent notification in notification panel.因此,您需要创建一个前台服务并在通知面板中显示持久通知。 Then you can create a BroadcastReceiver from the Foreground Service.然后您可以从前台服务创建 BroadcastReceiver。

Launch your service as a foreground service using the code below使用以下代码将您的服务作为前台服务启动

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

And in service onCreate() make something like that:在服务 onCreate() 中做这样的事情:

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

Links below will give you a little more insight into Foreground Services.下面的链接将使您更深入地了解前台服务。

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

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

Create your BroadcastReceiver from the service.从服务中创建您的 BroadcastReceiver。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM