Domanda

I have two broadcast.

One is for screen on/off and second for phone state. I want to show my activity to user if screen is on but not if it is phone call.

How to manage this any help will be appreciated..

È stato utile?

Soluzione

you need to use if/else condition to identify Broadcast action as:

public static final String Screen_on = "android.intent.action.SCREEN_ON";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Screen_on)) {
   // do your job here if screen is on /off
}
else{
        // send new intent to your Activity here 
         Intent intent = new Intent(context,Activity1.class);
     intent.putExtra("status","CALL_STATE");
     context.startActivity(intent);

        Bundle extras = intent.getExtras();
       if (extras != null) {
        String state = extras.getString(TelephonyManager.EXTRA_STATE);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING) {
           // do your job here if PHONE STATE CHNAGED
        }
      }

}

and declare your Activity in AndroidManifest.xml as android:launchMode="singleTask" and override onNewIntent to recive intent when Activity running as :

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    finishactivity();
}
private void finishactivity(){
    Intent intent = getIntent();
    Bundle extras=intent.getExtras();
    if(extras!=null){
          if(extras.getString("status").equals("CALL_STATE"))
         // finish your Activity here
    } 
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top