【问题标题】:Android Broadcast Receiver in Service服务中的 Android 广播接收器
【发布时间】:2015-11-04 19:54:59
【问题描述】:

我正在尝试提供每次电话结束时都会执行某些操作的服务。我的问题是服务没有收到任何广播。 我是这样实现的:

    package com.kubasienki.spedycja;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

/**
 * Created by kuba on 11/4/2015.
 */

    public class FirstService extends Service {

        private static String TAG = "kurwa";
    final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals("android.provider.Telephony.CALL_STATE_IDLE")){
                Log.v("kurwa", "skonczono");
            }
            else if(action.equals(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
                Log.v("kurwa", "???");
            }
            else {
                Log.v("kurwa", "??!!!?");

            }
        }

    };

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub

            super.onStart(intent, startId);
            Log.d(TAG, "FirstService started");


            IntentFilter filter = new IntentFilter();
            filter.addAction("android.provider.TelephonyManager.CALL_STATE_IDLE");
            filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED);


            registerReceiver(receiver, filter);
            //this.stopSelf();
        }

        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Log.d(TAG, "FirstService destroyed");
        }

    }

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kubasienki.spedycja" >

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" >
        <uses-permission android:name="android.permission.CALL_PHONE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
        <activity android:name="com.kubasienki.spedycja.MainActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".FirstService" ></service>
    </application>

</manifest>

开始服务:

startService(new Intent(this, FirstService.class));

【问题讨论】:

  • 使用Log.v("kurwa", 不是一个好习惯,最好像这样使用TAGprivate final static String TAG = "kurwa";

标签: android service broadcastreceiver telephony


【解决方案1】:

创建一个监听呼叫的广播

class CallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {//do want you want
    }
}

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />  
 <receiver android:name=".CallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

Handling Phone Call Requests the Right Way for Users

【讨论】:

  • 我仍然没有像你写的那样上课,然后在我的服务课程中像final BroadcastReceiver receiver = new CallReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.provider.TelephonyManager.CALL_STATE_IDLE"); filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED); registerReceiver(receiver, filter);一样使用它
  • 用户拨号时能否连接到app的暂停状态? @Vishal
  • 每当您拨打电话时,CallReceiver Broadscat 都会收到通知
  • 但是如果它在清单中注册,我应该从我的服务中注册该类吗??
  • 每次打电话时都会收到通知。你不需要添加 ..final BroadcastReceiver receiver = new CallReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.provider.TelephonyManager.CALL_STATE_IDLE"); filter.addAction(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED); registerReceiver(接收器,过滤器);
【解决方案2】:

权限放错地方了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-23
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多