3

アプリはSMSメッセージを傍受しDialog、メッセージのを表示します。

ただし、クラスDialogでエラーを解決できません。Test私は何が間違っているのですか?

(他の2つのファイルも含めました)。

Eclipseに表示されるエラー: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;
    }  
}
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.BuilderDialog次に、を使用してビルドできますbuilder.create()Dialog次に、で表示できます.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 に答える