我已经尝试了很长时间来处理执行以下
的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);
}
}
注意:电话号码属于运行该应用程序的手机,因此应在收到回复后打开谷歌
所有使用权限也都已到位。
更新:我发现了进度对话框的问题,&#34; O&#34; OnPreExecute中的字母应该是小写字母。现在显示进度对话框并且应用程序发送消息,但是一旦收到回复短信,应用程序就会崩溃。我该如何解决这个问题?
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
答案 0 :(得分:0)
不确定,但是从以下一行
ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.google.com ...
我认为您的网址应该像http://www.google.com
。它缺少协议http
,如果没有它,它可能无法识别处理Intent
的正确活动。