3

When phone state is ringing, I want to run an activity to show my own screen.

I'm using:

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

and

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

but notification bar does not hide when activity is shown.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Micle
  • 159
  • 10

1 Answers1

0

Go through following code snippets:

  1. Create a BroadCastReceiver Class to listen to incoming calls with the highest priority:

    Manifest.xml:

    <receiver android:name=".MyPhoneBroadcastReceiver">
        <intent-filter android:priority="99999">
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
    
  2. Then in class, onReceive() method opens the CustomCallsReceiver Activity with intent action is "android.intent.action.ANSWER"

    @Override
    public void onReceive(final Context context, Intent intent) {
    
    Bundle extras = intent.getExtras();
    
    if (extras != null) {
    
        String state = extras.getString(TelephonyManager.EXTRA_STATE);          
        final String incomingNumber = extras.getString("incoming_number");
    
        Handler callActionHandler = new Handler();
    
        Runnable runRingingActivity = new Runnable(){
            @Override
            public void run() {
                 //Notice the intent, cos u will add intent filter for your class(CustomCallsReceiver)
                Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
                intentPhoneCall.putExtra("INCOMING_NUM", incomingNumber);
                intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentPhoneCall);
            }
        };
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            //increase the delay amount if problem occur something like -the screen didn't show up- that's the key about this method(the delay).
            callActionHandler.postDelayed(runRingingActivity, 100);    
        }
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            callActionHandler.removeCallbacks(runRingingActivity);
        }
    
    }
    }
    
  3. Add this intent filter in to manifest file for the class you will use as a Custom call receiver.

        <activity android:name="CustomCallsReceiver" android:noHistory="true" android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.ANSWER" />
             <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>    
    </activity>
    
  4. The CustomeCallsReceiver Class:

    public class CustomCallsReceiver extends Activity {
    
     private String TAG = "CustomCallsReceiver";
     String incomingNumber, caller;
    
     @Override
       public void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custome_calls_receiver);
    
    TextView number = (TextView) findViewById(R.id.number);
    number.setGravity(Gravity.CENTER);
    
    incomingNumber = getIntent().getExtras().getString("INCOMING_NUM");
    caller = getCallerName(incomingNumber);
    
    if (caller != null) {
        number.setText(caller + "\n" + incomingNumber);  }  
            }
    
  5. And finally of course don't forget to add the Theme for not title or notification bar at the manifest file

     <application
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    

Hope this working for you...

Swapnil Sonar
  • 2,232
  • 2
  • 29
  • 42
  • tanks,but i create thread like this: public void run(){ Intent i = new Intent(); i.setClass(con, Call.class);...} and activity show correctly but notification bar in many times show in activity(sometimes hidden)! – Micle Aug 20 '13 at 14:06