【问题标题】:Android Network connection statusAndroid 网络连接状态
【发布时间】:2012-08-22 03:24:24
【问题描述】:

我正在尝试使用 BroadcastReceiver 建立此网络连接。当连接不存在时,它会显示一个警告对话框,告诉用户重试连接。我无法让这部分工作。它说 "The constructor AlertDialog.Builder(ConnectionDetector) is undefined" 和 "The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (ConnectionDetector, String, int)" 。我想我搞砸了我的代码。最后一个问题是如何做一个重试按钮?这里对 Android 来说很新。谢谢

   import android.app.AlertDialog;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.widget.Toast;



    /**
     * This broadcast receiver is awoken after boot and registers the service that
     * checks for new photos from all the known contacts.
     */


   public class ConnectionDetector extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {



    boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false);

     if(noConnectivity){

             Toast.makeText(context, "Internet Connection Required" , Toast.LENGTH_LONG).show();


             AlertDialog.Builder builder = new AlertDialog.Builder(context);
             builder.setCancelable(true);
            // builder.setIcon(R.drawable.dialog_question);
             builder.setTitle("Title");
             builder.setInverseBackgroundForced(true);
             builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                 dialog.dismiss();
               }
             });
             builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                 dialog.dismiss();
               }
             });
             AlertDialog alert = builder.create();
             alert.show();

         //System.exit(0);
         //Show Warning Message
         //Close Application the way i suggested
     }

    }

    }

Logcat:应用程序崩溃

 08-22 13:56:31.899: E/AndroidRuntime(29026): FATAL EXCEPTION: main
    08-22 13:56:31.899: E/AndroidRuntime(29026): java.lang.RuntimeException: Unable to start receiver com.example.androidhive.ConnectionDetector: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2140)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.app.ActivityThread.access$1500(ActivityThread.java:127)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.os.Handler.dispatchMessage(Handler.java:99)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.os.Looper.loop(Looper.java:137)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.app.ActivityThread.main(ActivityThread.java:4512)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at java.lang.reflect.Method.invokeNative(Native Method)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at java.lang.reflect.Method.invoke(Method.java:511)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at dalvik.system.NativeStart.main(Native Method)
    08-22 13:56:31.899: E/AndroidRuntime(29026): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:693)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.app.Dialog.show(Dialog.java:278)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at com.example.androidhive.ConnectionDetector.onReceive(ConnectionDetector.java:52)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2133)
    08-22 13:56:31.899: E/AndroidRuntime(29026):    ... 10 more

【问题讨论】:

  • 标题错误,不是你要的。

标签: android connection broadcastreceiver network-state


【解决方案1】:

这是因为您试图传递错误的上下文

替换

AlertDialog.Builder builder = new AlertDialog.Builder(this);

通过

AlertDialog.Builder builder = new AlertDialog.Builder(context);

替换

Toast.makeText(this, "Internet Connection Required" , Toast.LENGTH_LONG).show();

Toast.makeText(context, "Internet Connection Required" , Toast.LENGTH_LONG).show();

【讨论】:

  • 您好 Vipul,当我输入该代码时,我的应用程序崩溃了。请指导我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
相关资源
最近更新 更多