广播接收器不工作

时间:2011-07-09 21:28:48

标签: android broadcastreceiver intentfilter

我正在尝试测试广播接收器的拨出电话。请帮忙。

这是我的CallCounter类:

package com.callout;

import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;


 public class CallCounter extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) {
    switch(state) {
        case TelephonyManager.CALL_STATE_IDLE:
                Log.d("Tony+++++++++++","Outgoing Call finished");
                // Call Finished -> stop counter and store it.
   //             callStop=new Date().getTime();
               Context c = getApplicationContext();

              c.stopService(new Intent(c,ListenerContainer.class));

            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("++++++++++++++++Tony","Outgoing Call Starting");
                // Call Started -> start counter.
                // This is not precise, because it starts when calling,
                // we can correct it later reading from call log
      //          callStart=new Date().getTime();
            break;
    }
}

private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return this.getApplicationContext(); 

}
 }

这是myReceiver

 package com.callout;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.util.Log;

  public class myReceiver extends BroadcastReceiver {
  public void onReceive(Context context, Intent intent) {
      if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
         Log.d("T+++++++++++++++++ony","In mYRecieverrr");
         context.startService(new Intent(context,ListenerContainer.class));
      }
  }
  }

这是我的服务:

 package com.callout;

 import android.app.Service;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Binder;
 import android.os.IBinder;
 import android.telephony.PhoneStateListener;
 import android.telephony.TelephonyManager;
 import android.util.Log;

  public class ListenerContainer extends Service {
     public class LocalBinder extends Binder {
       ListenerContainer getService() {
         Log.d("Tony","OKKK HEREEEEEEEEEEEEEeEEEEE");
          return ListenerContainer.this;
       }
   }
  @Override
   public void onStart(Intent intent, int startId) {
      TelephonyManager tManager    =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

         CallCounter callCounter=new CallCounter();
        tManager.listen(callCounter,PhoneStateListener.LISTEN_CALL_STATE);

       Log.d("To++++++++++++++ny","Call COUNTER Registered");
   }
   @Override
   public IBinder onBind(Intent intent) {
       return mBinder;
    }

private final IBinder mBinder = new LocalBinder();
}

如果我遗漏了一些许可,这就是清单文件:

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.callout"
     android:versionCode="1"
    android:versionName="1.0">
   <uses-sdk android:minSdkVersion="8" />
   <uses-permission android:name="android.permission.READ_CONTACTS" />
   <uses-permission android:name="android.permission.CALL_PHONE"/>
   <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".CallCounter"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
       </activity>
          <activity android:name=".myReceiver"
              android:label="@string/app_name">
            <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
           </activity>
                <service android:enabled="true" android:name=".ListenerContainer" />
            <receiver android:name=".myReceiver">
            <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
        </receiver>
   </application>

 </manifest>

请指出我的错误。

3 个答案:

答案 0 :(得分:2)

您必须在清单中添加以下权限,因为您正在注册一个phonestatelistener。

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

答案 1 :(得分:1)

看起来您的接收器缺少清单文件中的属性:

http://developer.android.com/guide/topics/manifest/receiver-element.html

查看导出的属性,应将其设置为true以从其他应用程序接收广播。

此致   斯特凡

答案 2 :(得分:0)

这里有一个完整来源的工作解决方案,符合您的要求

https://www.codeproject.com/articles/548416/detecting-incoming-and-outgoing-phone-calls-on-and