Android 在服务运行时创建新活动
如何在检测到来电时启动新活动。在下面的代码中,我想在Android 在服务运行时创建新活动,android,telephony,Android,Telephony,如何在检测到来电时启动新活动。在下面的代码中,我想在CALL\u STATE\u RINGINGSTATE开始新的活动 public String getCurrentCallState(final TelephonyManager mytelMgr) { int callState = mytelMgr.getCallState(); String callStateString = "NOTKNOWN"; switch (callState)
CALL\u STATE\u RINGING
STATE开始新的活动
public String getCurrentCallState(final TelephonyManager mytelMgr) {
int callState = mytelMgr.getCallState();
String callStateString = "NOTKNOWN";
switch (callState) {
case TelephonyManager.CALL_STATE_IDLE:
callStateString = "IDLE";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callStateString = "OFFHOOK";
break;
case TelephonyManager.CALL_STATE_RINGING:
callStateString = "RINGING";
break;
}
}
现在我在*呼叫状态\u响铃*状态中做了一些更改,但它不起作用。无论何时来电,我的应用程序都将退出。
实际上,我想在来电铃声响起时呼叫该活动
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class CallHelper extends Activity
{
/**
* Listener to detect incoming calls.
*/
private class CallStateListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
Intent i = new Intent(getApplicationContext(), out.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //must be provided
getApplicationContext().startActivity(i);
Toast.makeText(ctx, "Incoming: "+incomingNumber, Toast.LENGTH_LONG).show();
break;
}
}
}
/**
* Broadcast receiver to detect the outgoing calls.
*/
public class OutgoingReceiver extends BroadcastReceiver {
public OutgoingReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(ctx, "Outgoing: "+number, Toast.LENGTH_LONG).show();
}
}
private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
private OutgoingReceiver outgoingReceiver;
public CallHelper(Context ctx) {
this.ctx = ctx;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
/**
* Start calls detection.
*/
public void start() {
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
ctx.registerReceiver(outgoingReceiver, intentFilter);
}
/**
* Stop calls detection.
*/
public void stop() {
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
ctx.unregisterReceiver(outgoingReceiver);
}
}
从
服务
甚至广播接收器
启动活动
时,除了一个重要标志外,没有任何区别。在您的通话状态中使用以下选项:
确保您的启动活动也在AndroidManifest中定义
Intent i = new Intent(getApplicationContext(), YourActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //must be provided
getApplicationContext().startActivity(i);