问题
The app intercepts sms messages and displays a Dialog
of the message.
However I am unable to get my Dialog
error resolved in my Test
class. What am I doing wrong?
(I've also included my other 2 files).
ERROR shown in 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;
}
}
回答1:
Do this:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Message")
.setMessage(str)
.setNeutralButton("OK", null);
AlertDialog dialog = builder.create();
dialog.show();
Instead of:
AlertDialog show = new AlertDialog.Builder(this)
.setTitle("Message")
.setMessage(str)
.setNeutralButton("OK", null)
.show();
You have to create an instance of an AlertDialog.Builder
first. Then you can build the Dialog
with builder.create()
. Then you can show the Dialog
with .show()
.
回答2:
I'm new to android but found that sometimes you cant create an alert inside a class that is not an activity. you should then get the context directly and create the alert.
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();
}
回答3:
You can't use dialog on BroadcastReceiver, so instead you better call an activity for the dialog box from the BroadcastReceiver,
add this code in your onReceive function :
@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);
}
fill the {CLASSNAME} with the dialog activity, heres my dialog activity :
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();
}
}
where I got the answer ?, here : How do you use an alert dialog box in a broadcast receiver in android? thanks to Femi !!, I just spread the news :D,
greetings from Indonesia.
回答4:
Write this code instead. I tested it.
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();
}
来源:https://stackoverflow.com/questions/11585099/alertdialog-show-new-alertdialog-builderthis-is-undefined