3

该应用程序拦截短信并显示一条Dialog消息。

但是,我无法Dialog在课堂上解决我的错误Test。我究竟做错了什么?

(我还包括了我的其他 2 个文件)。

Eclipse 中显示的错误:AlertDialog.Builder(Test) is undefined


测试.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>

你好.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;
    }  
}
4

4 回答 4

8

做这个:

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。然后你可以构建Dialogwith builder.create()。然后你可以显示Dialogwith .show()

于 2012-07-20T18:41:57.253 回答
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();
}
于 2014-04-23T19:27:11.803 回答
0

您不能在 BroadcastReceiver 上使用对话框,因此您最好从 BroadcastReceiver 调用对话框的活动,

在您的 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();
}
}

我在哪里得到答案?,在这里:如何在 android 的广播接收器中使用警报对话框? 感谢 Femi !!,我刚刚传播了这个消息:D,

来自印度尼西亚的问候。

于 2013-08-27T10:21:41.183 回答
0

改为编写此代码。我测试了它。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);



    Button btn=(Button)findViewById(R.id.btnLogin);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ShowMessage();
        }
    });
}

protected void ShowMessage(){
    AlertDialog show = new AlertDialog.Builder(this)
            .setTitle("Message")
            .setMessage("khguh")
            .setNeutralButton("OK", null)
            .show();
}
于 2019-08-20T10:42:44.960 回答