该应用拦截短信息并显示该消息的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;
}
}
答案 0 :(得分:6)
这样做:
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
的实例。然后,您可以使用Dialog
构建builder.create()
。然后,您可以使用Dialog
显示.show()
。
答案 1 :(得分: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();
}
答案 2 :(得分: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();
}
}
我得到了答案?,在这里:How do you use an alert dialog box in a broadcast receiver in android? 感谢Femi !!,我只传播新闻:D,
来自印度尼西亚的问候。
答案 3 :(得分: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();
}