2011-07-09 120 views
0

我想测试广播接收器的传出呼叫。请帮忙。广播接收器不工作

这里是我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)); 
     } 
    } 
    } 

这里是为myService:

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

你希望我们能够在我们的水晶球看起来可行的解决方案?你得到什么错误,LogCat显示什么或不工作? –

+1

跟踪你的代码。 – Snicolas

+0

对不完整的问题,错误地删除了日志部分。 Receiver工作于F9,然后尝试启动该服务。当我追踪我的代码时,我发现它直到onstart()方法,但在此之后不会启动phonestatelistener部分。这是一种错误:/ AndroidRuntime(288):致命异常:主E/AndroidRuntime(288):java.lang.RuntimeException:无法启动服务[email protected] Intent {cmp = com.callout/.ListenerContainer}:java.lang.SecurityException:用户10042和当前进程都没有Android.permission.READ_PHONE_STATE – Swati

回答

1

它看起来像你的接收器丢失清单文件属性:

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

看导出的属性,它应该被设置TOT true时从其他应用程序接收广播。

问候, 斯特凡

+0

@Stephane:谢谢你的回答,但没有收到来自其他应用程序的广播,只是全局广播。在我的情况下,服务工作正常,但phonestatelistener没有实例化。请帮助 – Swati

+0

你试过了吗?在我看来,全球广播恰恰意味着不是应用程序本地广播... – Snicolas

2

,因为你是注册phonestatelistener您必须添加以下权限到您的清单。

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