我们接到电话时如何将活动调用到前台?

时间:2014-03-10 11:49:26

标签: android broadcastreceiver call

我陷入了困境。我想要实现的是在有人呼叫我的手机时将视频设置为来电显示。

以下是我的示例代码:

public class MainActivity extends Activity {

VideoView videoView;
TelephonyManager telephonyManager;
String videopath= Environment.getExternalStorageDirectory() + "/"
        + "video.mp4";
//MediaController mediaController;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    videoView = (VideoView) findViewById(R.id.videoView1);
    //mediaController = new MediaController(this);
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    PhoneStateListener psl = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            // TODO Auto-generated method stub

            if (state == TelephonyManager.CALL_STATE_RINGING) {
                Intent intent2open = new Intent(MainActivity.this,
                        MainActivity.class);
                intent2open.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
                        | Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP
                        | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                intent2open.setAction("android.intent.action.VIEW");
                intent2open.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent2open.setAction(Intent.ACTION_MAIN);
                intent2open.addCategory(Intent.CATEGORY_LAUNCHER);
                intent2open.setFlags(0);
                sendBroadcast(intent2open);
                videoView.setVideoPath(videopath);

                videoView.start();
                videoView.bringToFront();
                Toast.makeText(getApplicationContext(),
                        "Incoming call" + incomingNumber, Toast.LENGTH_LONG)
                        .show();
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };
    telephonyManager.listen(psl, PhoneStateListener.LISTEN_CALL_STATE);

}

这是我的广播接收者代码:

public class CallReceiver extends BroadcastReceiver {

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


    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    Toast.makeText(context, state, 0).show();
    if(state.equals(TelephonyManager.CALL_STATE_RINGING))
    {
        Intent intent2 = new Intent(context,MainActivity.class);
        intent2.putExtras(intent2);
        intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent2);

    }
}
}

任何人都可以帮我找一下这段代码的错误吗?

0 个答案:

没有答案