应用程序拦截短信并显示消息的Dialog
。
但是,我无法在Test
类中解决Dialog
错误。我做错了什么?
(我还包括了另外两个文件)。
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>
你好.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;
}
}
这样做:
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
。
我是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();
}
您不能在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,
来自印度尼西亚的问候。
改为编写此代码。我测试过了。
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();
}