广播接收机和服务don';应用程序关闭时无法工作(android)

广播接收机和服务don';应用程序关闭时无法工作(android),android,android-service,android-broadcastreceiver,Android,Android Service,Android Broadcastreceiver,对不起大家。首先,由于自动启动管理器,当应用程序在我的设备上关闭时,我的接收器不工作。我觉得自己很愚蠢。。。当我试图解决这个问题时,我学到了非常重要的东西 首先是安卓6.0权限请求 第二:在棒棒糖上播放Android复制的手机状态 谢谢 我是Android新手,我想学习广播接收器和服务。此代码广播接收器监听挂机和空闲状态我的手机和发送意图服务做一些事情。当应用程序运行正常时。关闭应用程序后,接收器和服务不工作 更新:我还注意到,当应用程序正常运行时,服务启动两次,停止两次 我在服务启动时看到

对不起大家。首先,由于自动启动管理器,当应用程序在我的设备上关闭时,我的接收器不工作。我觉得自己很愚蠢。。。当我试图解决这个问题时,我学到了非常重要的东西

首先是安卓6.0权限请求

第二:在棒棒糖上播放Android复制的手机状态

谢谢

我是Android新手,我想学习广播接收器和服务。此代码广播接收器监听挂机和空闲状态我的手机和发送意图服务做一些事情。当应用程序运行正常时。关闭应用程序后,接收器和服务不工作

更新:我还注意到,当应用程序正常运行时,服务启动两次,停止两次

我在服务启动时看到以下消息:

  • Toast.makeText(上下文“+phoneState+”+记录,Toast.LENGTH_SHORT.show()

  • Toast.makeText(getApplicationContext(),“服务工作”,Toast.LENGTH_SHORT).show()

  • 再一次1。Toast.makeText(上下文“+phoneState+”+记录,Toast.LENGTH_SHORT.show()

    再次2。Toast.makeText(getApplicationContext(),“服务工作”,Toast.LENGTH\u SHORT.show())

    我在服务停止时看到以下消息:

  • Toast.makeText(上下文“+phoneState+”+记录,Toast.LENGTH_SHORT.show()

  • Toast.makeText(getApplicationContext(),“服务将停止。”,Toast.LENGTH_SHORT.show()

  • 再一次1。Toast.makeText(上下文“+phoneState+”+记录,Toast.LENGTH_SHORT.show()

    再次2。Toast.makeText(getApplicationContext(),“服务将停止。”,Toast.LENGTH_SHORT.show()

    一切都重复

    舱单:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="devapp.deneme">
    
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:enabled="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>
    
            <service
                android:name=".MyService"
                android:enabled="true"
                android:exported="true" />
    
            <receiver
                android:name="devapp.deneme.MyReceiver"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
                    <action android:name="android.intent.action.PHONE_STATE"/>
                </intent-filter>
            </receiver>
        </application>
    
    </manifest>
    
    服务

    package devapp.deneme;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.widget.Toast;
    
    public class MyService extends Service {
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        public int onStartCommand (Intent intent, int flags, int startId){
    
            boolean record = intent.getBooleanExtra("record", false);
            if (record) {
                Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show();
                stopSelf();
            }
            return super.onStartCommand(intent, flags, startId);
        }
    }
    

    以粘性启动服务。更改您的
    onStartCommand
    服务方法,如下所示

    public int onStartCommand (Intent intent, int flags, int startId){
    
        boolean record = intent.getBooleanExtra("record", false);
        if (record) {
            Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show();
            stopSelf();
        }
         return START_STICKY;  // or START_REDELIVER_INTENT or START_STICKY_COMPATIBILITY
    }
    

    您必须在服务类中添加以下内容

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
               // your business logic 
    
            return START_STICKY;
    }
    

    如果你说的关闭是指强制关闭,那么它将不起作用。使用任务管理器幻灯片右键或左键,这一切都与api级别和权限有关:我已经工作了2天了。我以前试过。我认为接收者有问题。但我不能解决。我还尝试了两种不同的设备。这都与api级别和权限有关:
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
               // your business logic 
    
            return START_STICKY;
    }