【发布时间】:2014-05-26 14:41:12
【问题描述】:
我在一个应用程序上工作,它发送一条短信并将收到的短信显示为AlertDialog,但在接收短信时不显示 AlertDialog
这是我项目的代码:
主活动:
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;
}
}
主布局:
<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;
}
}
错误在公共 void 方法中。 SmsReceiver 中有两次错误,但这个问题是针对这个问题的! 你在这里@faizal
提前致谢
【问题讨论】:
-
你也可以发布布局文件吗?
-
你在manifest中注册了receiver吗?
-
@faizal 为您编辑!
-
@user184994 是的,我注册了!
-
@user184994 亲爱的我有 3 个活动:1-splashScreen 2-MainActivity 3-SmsReceiver 我在第一个活动(SplashScreen)下注册了它,你能帮帮我吗?
标签: java android android-alertdialog