2011-02-05 67 views
0

我看到很多應用程序做短信彈出窗口。爲什麼我無法使我的應用程序工作?如果有短信息,我希望它在屏幕上彈出。短信彈出:AlertDialog不顯示,因爲我收到短信

這裏是我的代碼:

public class NotifySMSReceived extends Activity 
{ 

    private static final String LOG_TAG = "SMSReceiver"; 

    public static final int NOTIFICATION_ID_RECEIVED = 0x1221; 

    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 


    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 



     IntentFilter filter = new IntentFilter(ACTION); 

     this.registerReceiver(mReceivedSMSReceiver, filter); 

    } 


    private void displayAlert() 

    { 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 

     builder.setMessage("Are you sure you want to exit?").setCancelable(

       false).setPositiveButton("Yes", 
       new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }).setNegativeButton("No", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 

    private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      if (ACTION.equals(action)) 
      { 
       //your SMS processing code 
       displayAlert(); 
      } 
     } 
    };  
} 
+0

當收到新的SMS消息時,有一些移動部件可以顯示彈出窗口。你可以發佈你的Android Manifest以及它是如何工作的一個重要部分。 – 2011-08-25 16:38:52

回答

0

我覺得這裏的問題是與上下文對象。來自

的onReceive(上下文語境,意圖意圖)

你應該通過上下文onRecive收到喜歡 ..

私人無效displayAlert(上下文語境

然後, 變化,

AlertDialog.Builder助洗劑=新AlertDialog.Builder(本);

TO

AlertDialog.Builder助洗劑=新AlertDialog.Builder(上下文);

現在它應該work.hope這有助於。

乾杯。

+0

我做了改變AlertDialog仍然不彈出烤麪包 – 2011-02-06 22:21:30