簡體   English   中英

進度對話框,Android中的廣播接收器崩潰

[英]Progress Dialog, Broadcast receiver crash in 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);
}

}

注意:該電話號碼屬於運行該應用程序的電話,因此在收到回復后應打開google

所有使用權限也都到位。

更新:我發現進度對話框存在問題,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 ...

我認為您的網址應類似於http://www.google.com 它缺少協議http ,否則它可能無法識別處理Intent的正確活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM