简体   繁体   English

applicaton关闭时,广播接收器和服务不起作用(android)

[英]Broadcast receiver and Service don't work when applicaton close (android)

Sory everyone. 大家好 Firstly my receiver doesnt work when application is closed on my devices because of AUTO START MANAGER. 首先,由于自动启动管理器而导致我的设备上的应用程序关闭时,我的接收器无法工作。 I feel stupid... And I learned very important things when I was trying to solve it. 我感到愚蠢……当我尝试解决它时,我学到了非常重要的事情。

Firstly Android 6.0 Permission Request Broadcast Receivers not working in Android 6.0 Marshmallow 首先,Android 6.0权限请求广播接收器无法在Android 6.0棉花糖中工作

Secondly: Android - duplicated phone state broadcast on Lollipop 其次:Android-在棒棒糖上播放重复的电话状态

http://www.skoumal.net/en/android-duplicated-phone-state-broadcast-on-lollipop/ http://www.skoumal.net/en/android-duplicated-phone-state-broadcast-on-lollipop/

Thanks... 谢谢...

I'm new on Android and I want to learn BroadcastReceiver&Service. 我是Android的新手,我想学习BroadcastReceiver&Service。 This codes BroadcastReceiver listen offhook and idle state my phone and send intent Service do something. 这段代码可以让BroadcastReceiver监听电话的摘机和空闲状态,并发送意图服务进行某些操作。 While application running everything normal. 当应用程序正常运行时。 After close application BroadcastReceiver and Service don't work. 关闭应用程序后,BroadcastReceiver和Service无法正常工作。

Update: Also I notice while application run and everythng normal, sevice were started twice and stopped twice. 更新:我还注意到当应用程序运行且一切正常时,服务启动了两次,并停止了两次。

I see this messages while service is starting: 服务启动时,我看到以下消息:

  1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show(); Toast.makeText(context,“” + phoneState +“” + record,Toast.LENGTH_SHORT).show();

  2. Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),“服务工作”,Toast.LENGTH_SHORT).show();

again 1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show(); 再次1. Toast.makeText(context,“” + phoneState +“” + record,Toast.LENGTH_SHORT).show();

again 2. Toast.makeText(getApplicationContext(), "service work", Toast.LENGTH_SHORT).show() 再次2. Toast.makeText(getApplicationContext(),“服务工作”,Toast.LENGTH_SHORT).show()

I see this messages while service is stoping: 服务停止时,我会看到以下消息:

  1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show(); Toast.makeText(context,“” + phoneState +“” + record,Toast.LENGTH_SHORT).show();

  2. Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(),“服务将停止。”,Toast.LENGTH_SHORT).show();

again 1. Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show(); 再次1. Toast.makeText(context,“” + phoneState +“” + record,Toast.LENGTH_SHORT).show();

again 2. Toast.makeText(getApplicationContext(), "service will be stopped.", Toast.LENGTH_SHORT).show(); 再次2. Toast.makeText(getApplicationContext(),“服务将停止。”,Toast.LENGTH_SHORT).show();

Everything repeat. 一切重复。

Manifest: 表现:

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

Broadcast Receiver 广播接收器

package devapp.deneme;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {

    public MyReceiver(){

    }

    Intent service = null;
    Bundle bundle = null;
    String phoneState = null;
    boolean record = false;

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

        service = new Intent(context, MyService.class);
        bundle = intent.getExtras();
        phoneState = bundle.getString(TelephonyManager.EXTRA_STATE);

        if (phoneState!=null){
            if ((phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))||(phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE))){
                service.putExtra("durum", phoneState);
                if (phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                    record = true;
                }
                else {
                    record = false;
                }
            }
            service.putExtra("record",record);
            Toast.makeText(context, "" +phoneState +" " +record, Toast.LENGTH_SHORT).show();
            context.startService(service);
        }
    }
}

Service 服务

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);
    }
}

Start the service as sticky. 粘性启动服务。 Change your onStartCommand method of service like following 更改服务的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
}

You have to add the following in service class 您必须在服务类中添加以下内容

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

           // your business logic 

        return START_STICKY;
}

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

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