如何使此电话呼叫状态广播接收器始终工作?(不停止)

huangapple go评论131阅读模式
英文:

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

问题

我正在创建一个应用程序,在呼叫状态之前和之后显示提示消息。我已经生成并安装了我的应用程序在我的手机上。在几天或有时几小时后它正常工作。 但之后它停止了,为了运行应用程序,我需要再次打开它。我如何修改我的应用程序以使其运行得很好?谢谢

清单

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

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

            }
        }
    }
}
英文:

I'm creating an application, which will show toast messages before and after the call states. 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

&lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_CALL_LOG&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.PROCESS_OUTGOING_CALLS&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;


    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;&lt;/activity&gt;
        &lt;activity android:name=&quot;.MainActivity2&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt; &lt;!-- This part is inside the application --&gt;
        &lt;receiver
            android:name=&quot;.CallReceiver&quot;
            android:enabled=&quot;true&quot;&gt;
            &lt;intent-filter android:priority=&quot;999&quot;&gt;
                &lt;action android:name=&quot;android.intent.action.PHONE_STATE&quot; /&gt;
                &lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot; /&gt;
                &lt;action android:name=&quot;android.intent.action.NEW_OUTGOING_CALL&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

> 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&lt;&gt;();

 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, &quot;1st&quot;, Toast.LENGTH_SHORT).show();

            } catch (Exception ex) {
                try {

                }
                catch (Exception e)
                {

                }
            }
        }
      }

答案1

得分: 1

新版本的Android不允许您实现您试图实现的操作,除非您创建前台服务。

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

因此,您需要创建一个前台服务,并在通知面板中显示持久通知。然后,您可以从前台服务中创建一个广播接收器。

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

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

从服务中创建您的广播接收器。

英文:

New versions of Android does not allow you to do what you are trying to achieve, unless you create foreground service.

Android does not want apps to run in background without explicit knowledge of the user.

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.

Launch your service as a foreground service using the code below

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

And in service onCreate() make something like that:

if (Build.VERSION.SDK_INT &gt;= 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-android

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

Create your BroadcastReceiver from the service.

huangapple
  • 本文由 发表于 2020年8月3日 17:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63227315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定