Ped*_*C88 1 android android-service
我想创建一个根据某些设置丢弃来电的应用,这在Android 1.6上似乎是不可能的.所以我决定编写一个应用程序,当呼叫被删除时,将Ringer更改为静音.问题是,当我调用getSystemService(Context.AUDIO_SERVICE)时,我得到一个异常.
这些是我的课程:
CallReceiver
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
Run Code Online (Sandbox Code Playgroud)
MyPhoneStateListener
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber){
if (state == TelephonyManager.CALL_STATE_RINGING)
{
Log.d("DEBUG", "RINGING");
(new TMLService()).ManageIncomingCall(incomingNumber);
}
}
}
Run Code Online (Sandbox Code Playgroud)
并且有一个名为TMLService的类,它扩展了包含此方法的Service
public void ManageIncomingCall(String incomingNumber)
{
super.onCreate();
AudioManager audioManage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
Run Code Online (Sandbox Code Playgroud)
就像我说的,当我调用AudioManager audioManage =(AudioManager)getSystemService(Context.AUDIO_SERVICE); 应用程序停止,这是我在LogCat中得到的:
D/DEBUG ( 356): RINGING
D/AndroidRuntime( 356): Shutting down VM
W/dalvikvm( 356): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime( 356): Uncaught handler: thread main exiting due to uncaught exception
D/CallNotifier( 103): RINGING... (new)
E/AndroidRuntime( 356): java.lang.NullPointerException
E/AndroidRuntime( 356): at android.content.ContextWrapper.getSystemService(ContextWrapper.java:335)
E/AndroidRuntime( 356): at tml.v1.Service.TMLService.ManageIncomingCall(TMLService.java:94)
E/AndroidRuntime( 356): at tml.v1.Service.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:14)
E/AndroidRuntime( 356): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:298)
E/AndroidRuntime( 356): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 356): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 356): at android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 356): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 356): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 356): at dalvik.system.NativeStart.main(Native Method)
D/CallNotifier( 103): onNewRingingConnection(): incoming
Run Code Online (Sandbox Code Playgroud)
Pet*_*yle 13
要在通话getSystemService(...)将无法工作之前onCreate()被Android框架调用.这在服务启动时发生(即通过[Context#bindService(...)] [1]或Context#startService(...)).我在尝试getSystemService()从构造函数调用时(即之前onCreate()被调用)看到了相同的NPE .
你只是打电话(new TMLService()).ManageIncomingCall(incomingNumber),这不允许Android初始化你的服务,这是这个NPE的根本原因.
为了使其正常工作,您必须启动该服务,然后在服务上调用方法.要调用方法,我认为你必须使用AIDL公开它.它可能比你需要的更复杂(也许?).
我听说IntentService是一种在没有AIDL复杂性的情况下在服务中执行操作的简单方法.以下是我认为IntentService应该如何工作的示例.没有测试过,但希望它对入门很有用.
CallReceiver
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener = new MyPhoneStateListener(context);
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
Run Code Online (Sandbox Code Playgroud)
MyPhoneStateListener
public class MyPhoneStateListener extends PhoneStateListener {
private final Context mContext;
public MyPhoneStateListener(Context context) {
this.mContext = context;
}
public void onCallStateChanged(int state, String incomingNumber){
if (state == TelephonyManager.CALL_STATE_RINGING)
{
Log.d("DEBUG", "RINGING");
// OPTION 1: Do it on the main thread (might be bad :) )
//AudioManager audioManage = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
//audioManage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
// OPTION 2: Use an IntentService (a bit easier than AIDL)
Intent intent = new Intent(TMLIntentService.ACTION_SILENCE_RINGER);
mContext.startService(intent);
}
}
}
Run Code Online (Sandbox Code Playgroud)
TMLIntentService
public class TMLIntentService extends IntentService {
public static final String ACTION_SILENCE_RINGER = "org.example.intentservice.ACTION_SILENCE_RINGER";
@Override
public void onHandleIntent(Intent intent) {
if(ACTION_SILENCE_RINGER.equals( intent.getAction() ) {
AudioManager audioManage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
}
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中
<service android:name=".TMLIntentService">
<intent-filter>
<action android:name="org.example.intentservice.ACTION_SILENCE_RINGER" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
[1]:http://d.android.com/reference/android/content/Context.html#bindService (android.content.Intent,android.content.ServiceConnection,int)
| 归档时间: |
|
| 查看次数: |
14281 次 |
| 最近记录: |