1

I am new to Android.

How Can I call the DialogFragment from my SmsReceiver extends BroadcastReceiver class What I am doing is checking if the user receives a text message. if yes then show the dialog box.

her is my code, thanks...

MainActivity

public class MainActivity extends ActionBarActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    ActionBar actionBar;

    // Declare Tab Variable
    ActionBar.Tab Tab1, Tab2, Tab3;
    Fragment fragmentTab1 = new FragmentTab1(); 
    Fragment fragmentTab2 = new FragmentTab2();
    Fragment fragmentTab3 = new FragmentTab3();


    public static Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context = getApplicationContext();


        //Hide Action Bar
        actionBar = getSupportActionBar();              
        // Hide Actionbar Icon
        actionBar.setDisplayShowHomeEnabled(true);
        // Hide Actionbar Title
        actionBar.setDisplayShowTitleEnabled(true);
        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setText("Asian");//.setIcon(R.drawable.tab1);
        Tab2 = actionBar.newTab().setText("Euro");
        Tab3 = actionBar.newTab().setText("Black");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        Tab3.setTabListener(new TabListener(fragmentTab3));

        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);
        actionBar.addTab(Tab3);






    }//-----end onCreate





    public static void confirmFireMissiles() {
        DialogFragment newFragment = new NotifyMessage();
        newFragment.show(getSupportFragmentManager(), "missiles"); 
    }
    //ERROR MSN: If i create a function in the Main Activity
    //Cannot make a static reference to the non-static method getSupportFragmentManager() from the type FragmentActivity






//Action bar of AppCombat -------------------------------------------------------------------------
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}//--end body

SmsReceiver i created this call

 MainActivity.confirmFireMissiles();

I also tried this, but dont work

Intent intent1 = new Intent();
                    intent1.setClass(context, NotifyMessage.class);
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent1);

Dialog class

public class NotifyMessage extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setMessage("You have a new msn")

               .setPositiveButton("ASSINAR", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!


                   }
               })
               .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog



                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }//--onCreateDialog



}//--body
Thiago
  • 12,778
  • 14
  • 93
  • 110
  • In your SMSReceiver, when you receive a text message you could send a broadcast that your activity is set up to receive, and then the activity can create the dialog: http://stackoverflow.com/questions/22241705/calling-a-activity-method-from-broadcastreceiver-in-android – JDJ Jul 01 '14 at 03:09

0 Answers0