电话的背景音乐

时间:2013-01-18 10:21:22

标签: android

我正在做一个在后台播放音乐的应用程序,我的应用程序有一个主要活动,一个广播监听器和一个服务。

广播监听器的代码是

public class CallBrdcstListener extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    PhoneStateDisplay phoneListener=new PhoneStateDisplay();
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

    telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}

电话状态监听器

   public class PhoneStateDisplay extends PhoneStateListener{

 private Context context;
public void onCallStateChanged(int state,String incomingNumber){
    switch(state)
    {
       case TelephonyManager.CALL_STATE_IDLE:
            Log.d("DEBUG", "IDLE");
            break;
       case TelephonyManager.CALL_STATE_OFFHOOK:

            Log.d("DEBUG", "OFFHOOK");
             System.out.println("Calling Intent");
            Intent musicIntent = new Intent(context,MusicService.class); 
            context.startService(musicIntent);  

            break;

       case TelephonyManager.CALL_STATE_RINGING:
           Log.d("DEBUG", "RINGING");
           break;
    }
}   
}

它在意图部分中显示错误...任何人都可以告诉我错误是什么

logcat输出

01-18 15:45:57.250: I/System.out(2822): Calling Intent
01-18 15:46:30.453: E/AndroidRuntime(2857): FATAL EXCEPTION: main
01-18 15:46:30.453: E/AndroidRuntime(2857): java.lang.NullPointerException
01-18 15:46:30.453: E/AndroidRuntime(2857):     at android.content.ComponentName.<init>(ComponentName.java:75)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at android.content.Intent.<init>(Intent.java:2823)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at com.example.backgroundmusicplay.PhoneStateDisplay.onCallStateChanged(PhoneStateDisplay.java:22)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:319)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at android.os.Looper.loop(Looper.java:130)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at android.app.ActivityThread.main(ActivityThread.java:3687)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at java.lang.reflect.Method.invoke(Method.java:507)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-18 15:46:30.453: E/AndroidRuntime(2857):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
 01-18 15:46:30.453: E/AndroidRuntime(2857):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

看这个https://stackoverflow.com/a/14393912/1697047 如果你想为你的应用程序播放背景音乐,那么在你的应用程序启动的线程中播放它/使用AsyncTask类为你做。

 public class BackgroundSound extends AsyncTask<Void, Void, Void> {
  @Override
 protected Void doInBackground(Void... params) {
 MediaPlayer player = MediaPlayer.create(YourActivity.this, R.raw.test_cbr); 
 player.setLooping(true); // Set looping 
 player.setVolume(100,100); 
 player.start(); 

 return null;
}

}