1

I want to add a button on incoming call screen. I am using a transparent screen with button and i calling this activity during incoming call but its not working properly. the code i used is..

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);
    }

}

I have used a separate activity for showing this...

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 is...

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
nawaab saab
  • 1,892
  • 2
  • 20
  • 36

1 Answers1

0

You are calling startActivity from class that doesn't extend Activity. Here's how you can fix this problem:

Add flag to Intent you are creating

Intent i = new Intent(Servo.this,Blank.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);


The exception is because you are trying to create UI component from class that it's not UI component itself.

vilpe89
  • 4,656
  • 1
  • 29
  • 36
  • thanks it worked but here's a very small problem the background is showing white and i took transparent, i want to show transparent background can you tell how can i do this?? – nawaab saab Apr 09 '14 at 08:44
  • 1
    open AndroidManifest.xml, find your activity from there and insert this in there `android:theme="@android:style/Theme.Translucent.NoTitleBar"` – vilpe89 Apr 09 '14 at 08:50
  • thanks dude its working but its giving problem when screen is locked, i have to firstly unlock the screen. what must i do so that i must not get this problem – nawaab saab Apr 09 '14 at 08:54
  • please solve a problem of mine when i am adding some more code it displays the transparent image when i cut the call...kindly look over this question of mine...??? – nawaab saab Apr 09 '14 at 12:29