Progress Dialog,android中的广播接收器崩溃



我已经尝试了很长时间来开发一个按钮onClick监听器,它执行以下

  • 启动加载屏幕(进度条)
  • 在后台向特定号码发送短信
  • 使用广播侦听器等待回复,这是一条包含链接的消息
  • 关闭加载屏幕
  • 然后使用此链接开始另一项活动

代码没有显示错误,但每次我按下按钮时,它都不会显示进度条,它只发送一条消息,然后等待指定的时间,然后崩溃。这是代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
@Override
`public void onClick(View arg0) {
// TODO Auto-generated method stub
new LoadTracker().execute();
}});
}

protected void sendSMS() {
// TODO Auto-generated method stub
String Phoneno = "***********";
String Message = "www.google.com";
SmsManager manager= SmsManager.getDefault();
manager.sendTextMessage(Phoneno, null, Message, null, null);
}
private BroadcastReceiver Receiver = new BroadcastReceiver(){
public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(smsBody));
startActivity(in);
}
}
}};
private class LoadTracker extends AsyncTask<Void, Integer, Void>{
protected void onPreExecute(){
progressDialog = new ProgressDialog(MainActivity.this);  
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("Sending Signal"); 
progressDialog.setMessage("Receiving Signal, Please Wait");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(false);  
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();  
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
synchronized (this){
IntentFilter IF = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");

sendSMS();
registerReceiver(Receiver,IF);
this.wait(10000);
this.wait(10000);
publishProgress(100); 
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... values){
progressDialog.setProgress(values[0]); 
}
@Override
protected void onPostExecute(Void result)  
{  
//close the progress dialog  
progressDialog.dismiss();  
//initialize the View  
setContentView(R.layout.activity_main);  
}
}
@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;
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(Receiver);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}

注意:电话号码属于运行应用程序的手机,因此它应该在收到回复后打开谷歌

所有的使用权限都已到位。

更新:我发现了进度对话框的问题,OnPreExecute中的"O"字母应该是小写的。现在进度对话框出现,应用程序发送消息。然而,一旦收到回复短信,应用程序就会崩溃。我该如何解决这个问题?

Logcat:

04-20 19:45:45.950: W/dalvikvm(15877): threadid=1: thread exiting with uncaught exception (group=0x40c4b1f8)
04-20 19:45:45.974: E/AndroidRuntime(15877): FATAL EXCEPTION: main
04-20 19:45:45.974: E/AndroidRuntime(15877): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x10 (has extras) } in com.example.finalprototype.MainActivity$1@4162c988
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:741)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.os.Handler.handleCallback(Handler.java:605)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.os.Looper.loop(Looper.java:137)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.app.ActivityThread.main(ActivityThread.java:4512)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invokeNative(Native Method)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at java.lang.reflect.Method.invoke(Method.java:511)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at dalvik.system.NativeStart.main(Native Method)
04-20 19:45:45.974: E/AndroidRuntime(15877): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com }
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1535)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1387)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.app.Activity.startActivityForResult(Activity.java:3190)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:817)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.app.Activity.startActivity(Activity.java:3297)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at com.example.finalprototype.MainActivity$1.onReceive(MainActivity.java:75)
04-20 19:45:45.974: E/AndroidRuntime(15877):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:732)
04-20 19:45:45.974: E/AndroidRuntime(15877):    ... 9 more
04-20 19:45:56.013: I/Process(15877): Sending signal. PID: 15877 SIG: 9

不确定,但来自下一行

ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com ...

我认为你的URL应该像http://www.google.com一样。它缺少协议http,如果没有该协议,它可能无法识别处理Intent的正确活动。

最新更新