谁能告诉我如何在服务在 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);
}
提前致谢!!!!!