【问题标题】:Broadcast receiver and Service don't work when applicaton close (android)应用程序关闭时广播接收器和服务不起作用(android)
【发布时间】:2016-07-25 19:22:39
【问题描述】:

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

首先是 Android 6.0 权限请求 Broadcast Receivers not working in Android 6.0 Marshmallow

其次:Android - 在 Lollipop 上复制手机状态广播

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

谢谢...

我是 Android 新手,我想学习 BroadcastReceiver&Service。此代码 BroadcastReceiver 监听摘机和空闲状态我的手机并发送意图服务做一些事情。虽然应用程序运行一切正常。关闭应用程序后,BroadcastReceiver 和 Service 不起作用。

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

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

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

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

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

再次 2. Toast.makeText(getApplicationContext(), "服务工作", Toast.LENGTH_SHORT).show()

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

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

  2. Toast.makeText(getApplicationContext(), "服务将被停止。", Toast.LENGTH_SHORT).show();

再次 1. Toast.makeText(context, "" +phoneState +" " +record, 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.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);
        }
    }
}

服务

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

【问题讨论】:

标签: android android-service android-broadcastreceiver


【解决方案1】:

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

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

           // your business logic 

        return START_STICKY;
}

【讨论】:

    【解决方案2】:

    以粘性启动服务。更改您的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
    }
    

    【讨论】:

    • 我已经解决了 2 天。我以前试过。在我看来,接收器有问题。但我无法解决。我也试过不同的两个设备。
    • 这都是关于 api 级别和权限的:stackoverflow.com/questions/33036523/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 2013-12-14
    相关资源
    最近更新 更多