服务在android中运行时弹出窗口
Posted
技术标签:
【中文标题】服务在android中运行时弹出窗口【英文标题】:Pop Up Window while service is running in android 【发布时间】:2013-03-18 14:39:37 【问题描述】:谁能告诉我如何在服务在 android 中运行时弹出一个窗口,这意味着,我正在开发一个应用程序,当在 android 手机中检测到来电时会弹出一个窗口。我搜索了很多,但没有找到任何确切的解释。所以请告诉我该怎么做。
我正在使用此代码。当我运行这个应用程序时,它会强制退出。
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.os.Bundle;
public class CallHelper extends Activity
/**
* Listener to detect incoming calls.
*/
public 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);
break;
/**
* Broadcast receiver to detect the outgoing calls.
*/
private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;
public CallHelper(Context ctx)
this.ctx = ctx;
callStateListener = new CallStateListener();
/**
* Start calls detection.
*/
public void start()
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
/**
* Stop calls detection.
*/
public void stop()
tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
//Intent intent = new Intent();
//startService(intent);
先谢谢了!!!!!!
【问题讨论】:
【参考方案1】:you can use the Broadcast receiver in your service in manifest.xml
---------------------------------------------
<receiver android:name="com.pericent.saferide.recciver.InComingCall" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
--------------------------------------------------
and when call is coming you call your activity in on receive method of BroadcastReciver
and you change your code listener like this
--------------------------------------------------
switch(state)
case CALL_STATE_RINGING:
checkCallAsUnreceived(incomingNumber);
break;
case CALL_STATE_OFFHOOK: //call was received
checkCallAsReceived();
break;
case CALL_STATE_IDLE: //no active calls
sendMsgIfCallWasntReceived();
break;
-------------------------------------------------------------
and for more detail you can see the below url:-
https://github.com/MarcinOS/AutoResponder
【讨论】:
以上是关于服务在android中运行时弹出窗口的主要内容,如果未能解决你的问题,请参考以下文章