我在一个应用程序上工作,发送短信并将收到的短信显示为AlertDialog
但是AlertDialog在收到短信时不显示
这是我的项目代码:
MainActivity:
package com.am7.masirinfo;
public class MainActivity extends Activity {
TextView tv;
SharedPreferences prefs;
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton btn2 = (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8A0441")));
bar.show();
bar.setIcon(R.drawable.bar);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayUseLogoEnabled(true);
bar.setDisplayHomeAsUpEnabled(false);
TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
tx.setTypeface(custom_font);
TextView tx1 = (TextView)findViewById(R.id.textView2);
Typeface custom_font1 = Typeface.createFromAsset(getAssets(), "fonts/gmail.ttf");
tx1.setTypeface(custom_font1);
TextView tx2 = (TextView)findViewById(R.id.textViewMessage);
tx2.setTypeface(custom_font);
TextView tx3 = (TextView)findViewById(R.id.textView3);
TextView tx4 = (TextView)findViewById(R.id.textView4);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
txtMessage = (EditText) findViewById(R.id.editTextSMS);
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
} protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try { SmsManager smsManager =
SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
AlertDialog ad1 = new AlertDialog.Builder(this).create();
ad1.setCancelable(false);
// This blocks the 'BACK' button
ad1.setMessage("با موفقیت ارسال شد");
ad1.setTitle("انجام شد");
ad1.setButton("باش", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad1.show();
ad1.setCanceledOnTouchOutside(true);
}
catch (Exception e) {
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false);
// This blocks the 'BACK' button
ad.setMessage("متاسفانه ارسال نشد مجددا تلاش کنید.");
ad.setTitle("خطایی رخ داد!");
ad.setButton("خب", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
ad.setCanceledOnTouchOutside(true);
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是SmsReceiver.java:
public class SmsReceiver extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if
(bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + phoneNumber + "; message: " + message);
if
(TextUtils.equals(currentMessage.getDisplayOriginatingAddress(), "+981000141")) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("اطلاعات ریافت شد!");
builder.setMessage(message);
builder.setPositiveButton("HEY!", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
}
});
builder.show();
}
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
private CharSequence contains(String string) {
// TODO Auto-generated method stub
return null;
}
}
Main_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<TextView
android:id="@+id/textViewMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sms_label" />
<EditText
android:id="@+id/editTextSMS"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="����: 0711 0511 ���� ����� �� ����"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSendSMS"
android:layout_width="fill_parent"
android:layout_height="41dp"
android:background="@drawable/abstract_877"
android:text="@string/send_sms_label" />
<EditText
android:id="@+id/editTextPhoneNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false"
android:inputType="phone"
android:text="+981000141"
android:visibility="invisible" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="����� �� ���� ��� ���� � Ӂ� �� �� � ����� �� ��� ���� �� ���� ������."
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textColorHint="#00BD39" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center"
android:text="AM7group@gmail.com" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="www.iran141.ir"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="+981000141"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="339dp"
android:background="@android:color/transparent"
android:src="@drawable/exitbtn" />
logcat的:
05-26 20:16:53.509: I/SmsReceiver(31910): senderNum: +981000141; message: عجبا
05-26 20:16:53.529: I/SmsReceiver(31356): senderNum: +981000141; message: عجبا
05-26 20:16:53.639: E/SmsReceiver(31356): Exception smsReceiverandroid.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
在与@faizal对话后,我更改了主要活动,但在MainActivity中遇到了2次错误:
public class MainActivity extends Activity {
TextView tv;
SharedPreferences prefs;
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
SmsReceiver BR_smsreceiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BR_smsreceiver = new SmsReceiver();
BR_smsreceiver.setActivityHandler(this);
IntentFilter fltr_smsreceived = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(BR_smsreceiver, fltr_smsreceived);
ImageButton btn2 = (ImageButton) findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8A0441")));
bar.show();
bar.setIcon(R.drawable.bar);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayUseLogoEnabled(true);
bar.setDisplayHomeAsUpEnabled(false);
public void showAlert(string message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(" !");
builder.setMessage(message);
builder.setPositiveButton("HEY!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
}
});
builder.show();
}
TextView tx = (TextView)findViewById(R.id.textView1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");
tx.setTypeface(custom_font);
TextView tx1 = (TextView)findViewById(R.id.textView2);
Typeface custom_font1 = Typeface.createFromAsset(getAssets(), "fonts/gmail.ttf");
tx1.setTypeface(custom_font1);
TextView tx2 = (TextView)findViewById(R.id.textViewMessage);
tx2.setTypeface(custom_font);
TextView tx3 = (TextView)findViewById(R.id.textView3);
TextView tx4 = (TextView)findViewById(R.id.textView4);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
txtMessage = (EditText) findViewById(R.id.editTextSMS);
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
} protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try { SmsManager smsManager =
SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
AlertDialog ad1 = new AlertDialog.Builder(this).create();
ad1.setCancelable(false);
// This blocks the 'BACK' button
ad1.setMessage("�� ������ ����� ��");
ad1.setTitle("����� ��");
ad1.setButton("���", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad1.show();
ad1.setCanceledOnTouchOutside(true);
}
catch (Exception e) {
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false);
// This blocks the 'BACK' button
ad.setMessage("�������� ����� ��� ����� ���� ����.");
ad.setTitle("����� �� ���!");
ad.setButton("��", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
ad.setCanceledOnTouchOutside(true);
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
错误是在public void方法中。 并且在SmsReceiver中出现两次错误,但是这个问题是针对这个问题的! 你在这里@faizal
提前致谢
答案 0 :(得分:0)
问题是提供给BroadcastReceiver的onReceive方法的上下文是应用程序上下文(而不是活动上下文),它不能用于显示对话框。因此,您需要在运行活动中而不是在广播接收器中显示对话框。
在SmsReceiver类中,创建一个函数来接收主活动类对象:
MainActivity act = null;
void setActivityHandler(MainActivity act)
{
this.act=act;
}
在主要活动中注册广播接收器(而不是在启动画面中)并调用setActivityHandler()。因此,您的主要活动中将包含以下几行:
SmsReceiver BR_smsreceiver = null;
onCreate(Bundle savedInstance){
....
BR_smsreceiver = new SmsReceiver();
BR_smsreceiver.setActivityHandler(this);
IntentFilter fltr_smsreceived = new
IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(BR_smsreceiver, fltr_smsreceived);
....
}
在SmsReceiver类中,删除所有AlertDialog构建器行并将其替换为
act.showAlert(message);
在您的主要活动课程中,创建showAlert():
public void showAlert(String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("اطلاعات ریافت شد!");
builder.setMessage(message);
builder.setPositiveButton("HEY!", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
}
});
builder.show();
}