2012-07-20 45 views
1

該應用程序攔截SMS消息並顯示消息的DialogAlertDialog show = new AlertDialog.Builder(this)未定義

但是我無法讓我的Dialog錯誤在我的Test類中得到解決。我究竟做錯了什麼?

(我還包括我的其他2個文件)。在Eclipse所示

ERROR:AlertDialog.Builder(Test) is undefined


test.java

package com.example.firstapp; 

import android.app.AlertDialog; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.SmsMessage; 

public class Test extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     //---get the SMS message passed in--- 
     Bundle bundle = intent.getExtras();   
     SmsMessage[] msgs = null; 
     String str = "";    
     if (bundle != null) 
     { 
      //---retrieve the SMS message received--- 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
      msgs = new SmsMessage[pdus.length];    
      for (int i=0; i<msgs.length; i++){ 
       msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
       str += "SMS from " + msgs[i].getOriginatingAddress();      
       str += " :"; 
       str += msgs[i].getMessageBody().toString(); 
       str += "\n";     
      } 

      AlertDialog show = new AlertDialog.Builder(this) 
      .setTitle("Message") 
      .setMessage(str) 
      .setNeutralButton("OK", null) 
      .show(); 
    }       
} 

}


的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.firstapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="9" 
     android:targetSdkVersion="15" /> 
    <uses-permission android:name="android.permission.SEND_SMS"> 
    </uses-permission> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"> 
    </uses-permission> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Hello" 
      android:label="@string/title_activity_hello" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name=".test"> 
      <intent-filter> 
       <action android:name= 
        "android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 

hello.java

package com.example.firstapp; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 

public class Hello extends Activity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_hello, menu); 
     return true; 
    } 
} 

回答

5

這樣做:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
builder.setTitle("Message") 
    .setMessage(str) 
    .setNeutralButton("OK", null); 

AlertDialog dialog = builder.create(); 
dialog.show(); 

相反的:

AlertDialog show = new AlertDialog.Builder(this) 
    .setTitle("Message") 
    .setMessage(str) 
    .setNeutralButton("OK", null) 
    .show(); 

您必須首先創建AlertDialog.Builder的實例。然後,您可以使用builder.create()構建Dialog。然後,您可以使用.show()顯示Dialog

+0

我仍然得到錯誤:AlertDialog.Builder(測試)是未定義 – thedeepfield 2012-07-20 18:44:47

+0

再試試吧,看看是否能工程。 – prolink007 2012-07-20 18:58:35

+0

感謝您的幫助〜 – thedeepfield 2012-07-20 19:53:06

0

不能在廣播接收器使用對話, 因此,你最好打電話從廣播接收器對話框的活動,

添加此代碼在您的onReceive功能:

@Override 
public void onReceive(Context context, Intent intent) 
{ 
    Intent i = new Intent(context, {CLASSNAME}.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 
} 

填補{} CLASSNAME與對話活動,我的繼承人對話活動:

package com.example.mbanking; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 


// ALERT DIALOG 
// Sources : http://techblogon.com/alert-dialog-with-edittext-in-android-example-with-source-code/ 

public class AlertDialogActivity extends Activity 
{ 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder 
     .setTitle("Test") 
     .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(); 
} 
} 

在那裏我得到了答案,在這裏:How do you use an alert dialog box in a broadcast receiver in android? 感謝Femi !!,我只是傳播消息:D,

來自印度尼西亞的問候。

2

我是新來的android,但發現有時你不能創建一個類不是一個活動的警報。 您應該直接獲取上下文並創建警報。

public void showAlert(String message){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); 
    builder.setTitle("Here is a message message from my activity") 
     .setMessage(message) 
     .setNeutralButton("OK", null); 
    AlertDialog dialog = builder.create(); 
    dialog.show(); 
} 
+0

對我來說,解決方案 – JMR 2015-10-10 16:10:34

相關問題