簡體   English   中英

啟用中的Android廣播接收器

[英]Android Broadcast Receiver in Service

我正在嘗試使每次電話結束時都會執行某些操作的服務。 我的問題是該服務沒有收到任何廣播。 我已經實現了它:

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

創建廣播以收聽呼叫

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>

處理電話請求為用戶提供了正確的方式

權限錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM