我想在来电屏幕上添加一个按钮。我正在使用带按钮的透明屏幕,我在来电期间调用此活动,但它无法正常工作。我使用的代码是..
public class Recevo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
Intent i= new Intent(context,Servo.class);
int state = tm.getCallState();
if(state==TelephonyManager.CALL_STATE_RINGING){
String number=intent.getStringExtra("incoming_number");
Log.i("State", "Call is Ringing");
context.startService(i);
}else if (state==TelephonyManager.CALL_STATE_IDLE) {
Log.i("State", "Idle State");
Toast.makeText(context, "Call is Idle", Toast.LENGTH_SHORT).show();
context.stopService(i);
} else if (state==TelephonyManager.CALL_STATE_OFFHOOK) {
//Log.i("State", "Call is Ringing");
//Toast.makeText(context, "Phone is Ringing", Toast.LENGTH_SHORT).show();
}
public class Servo extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
//Toast.makeText(getApplicationContext(), "in servo", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Servo.this,Blank.class);
startActivity(i);
}
}
我使用了一个单独的活动来展示这个......
public class Blank extends Activity implements OnClickListener{
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.blank);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Button Clicked", Toast.LENGTH_SHORT).show();
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/transparent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="147dp"
android:text="Button" />
</RelativeLayout>
Logcat是......
04-09 13:45:00.837: E/AndroidRuntime(3478): FATAL EXCEPTION: main
04-09 13:45:00.837: E/AndroidRuntime(3478): java.lang.RuntimeException: Unable to create service com.example.transparentscreen.Servo: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1976)
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.app.ActivityThread.access$2500(ActivityThread.java:121)
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:997)
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.os.Looper.loop(Looper.java:130)
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.app.ActivityThread.main(ActivityThread.java:3701)
04-09 13:45:00.837: E/AndroidRuntime(3478): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 13:45:00.837: E/AndroidRuntime(3478): at java.lang.reflect.Method.invoke(Method.java:507)
04-09 13:45:00.837: E/AndroidRuntime(3478): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
04-09 13:45:00.837: E/AndroidRuntime(3478): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
04-09 13:45:00.837: E/AndroidRuntime(3478): at dalvik.system.NativeStart.main(Native Method)
04-09 13:45:00.837: E/AndroidRuntime(3478): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.app.ContextImpl.startActivity(ContextImpl.java:652)
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
04-09 13:45:00.837: E/AndroidRuntime(3478): at com.example.transparentscreen.Servo.onCreate(Servo.java:22)
04-09 13:45:00.837: E/AndroidRuntime(3478): at android.app.ActivityThread.handleCreateService(ActivityThread.java:1966)
04-09 13:45:00.837: E/AndroidRuntime(3478): ... 10 more
04-09 13:45:04.897: I/Process(3478): Sending signal. PID: 3478 SIG: 9
04-09 13:45:12.087: I/State(3582): Idle State
答案 0 :(得分:0)
您正在从不会延长startActivity
的课程中致电Activity
。
以下是解决此问题的方法:
将标志添加到您正在创建的意图
Intent i = new Intent(Servo.this,Blank.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
例外情况是因为您尝试从类创建UI组件,而不是UI组件本身。