0

I am trying to make an android application which after getting a miss call gives a popup menu. I am using a BroadcastReceiver to listen to Phone state and PhoneStateListener to check if its a miss call or not , but how can I Prompt a popup menu after getting miss call ? :
I have tried using PopupMenu class but i don't know what to put in View argument.
I tried following code but it didn't worked :

package com.example.contact;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

public class GetText extends BroadcastReceiver {
Context con;
    @Override
    public void onReceive(Context context, Intent arg1) {
        con = context;
        View v = new View(context);
        Toast.makeText(context, "Got The message", Toast.LENGTH_LONG).show();
        PopupMenu popup = new PopupMenu(context,v);
        popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                Toast.makeText(con,
                        "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT)
                        .show();
                return true;
            }
        });
    }

}

Can anyone tell me a way to solve this... thank you in advance


Edit: *Dialog Class :*
import android.app.Activity;
import android.os.Bundle;

public class Dialog extends Activity {

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


}

In my Manifest :

<activity
            android:name="com.example.contact.Dialog"
            android:theme="@android:style/Theme.Dialog" >
        </activity>

Am I doing it in a wrong way?..please correct me if so...

Nawed Shaikh
  • 419
  • 5
  • 22
  • you can create and send Intent for start your special app (which consist of needed activities and popup menus) – xoxol_89 Apr 13 '14 at 17:28
  • I want the popup menu to appear without starting the main activity...is there a way I can do that? – Nawed Shaikh Apr 13 '14 at 17:31
  • 1
    only activities can show dialogs, menu and others. read [this](http://stackoverflow.com/questions/8766739/show-an-alert-dialog-in-broadcast-receiver-after-a-system-reboot) for example. – xoxol_89 Apr 13 '14 at 17:37

1 Answers1

0

I did something very similar with received SMS messages. Instead of a PopupMenu, use a Dialog themed Activity.

In your manifest, add the following to the <activity> tag:

android:theme="@android:style/Theme.Dialog"

Then, in the onReceive() method, start the Activity with an Intent:

Intent intent = new Intent(context, MenuActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

This will also give you greater control as far as follow-up functionality.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • After getting the message I want to prompt a popup menu which asks a yes or no question like : "Do you want to save this message?" then user can select from the option...so how can i do that? – Nawed Shaikh Apr 13 '14 at 17:46
  • Thank you :)..I will accept your answer but I want to ask if there is any way so that user can feel as if it is a popup menu??...i mean can we control the size of the layout so that user feels like it is a popup menu? – Nawed Shaikh Apr 13 '14 at 17:56
  • The buttons are not showing up in Dialog Theme is there any way I can show my button with the same style as Dialog? – Nawed Shaikh Apr 13 '14 at 18:47
  • It just shows the textView and sorry i don't know how to use setPositiveButton method... – Nawed Shaikh Apr 13 '14 at 19:00
  • I tried as you said to surround the textview and the upper button is showing but not the lower one and text is also not fully shown...so how can i make the size of the dialog bigger? – Nawed Shaikh Apr 13 '14 at 19:11
  • i added getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); After setcContentView and i get a full screen but still dont get that 2nd button – Nawed Shaikh Apr 13 '14 at 19:25
  • ahh! i got the problem!...i was using fill_parent for height and the textView was pushing the button down...Thank you all the way for helping you deserve more than an upvote :) – Nawed Shaikh Apr 13 '14 at 19:26