检查Internet连接SurfaceView

时间:2014-05-10 16:58:51

标签: android android-fragments

我正在使用surfaceview.iwrote代码女巫可以在faceview.i中播放来自url的视频使用Fragment.my代码可以在Wifi连接中播放视频,但3G连接我有错误这是我在清单文件中的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

这是一个网络连接类。

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context){
    this._context = context;
}

/**
 * Checking for all possible internet providers
 * **/
public boolean isConnectingToInternet(){
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null)
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null)
              for (int i = 0; i < info.length; i++)
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
      return false;
}

}

这是我的MainActivity java类的一部分

cd = new ConnectionDetector(getApplicationContext());




    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {



            if (!cd.isConnectingToInternet()) {
                Toast.makeText(getApplicationContext(),
                        "You did not have internet connection",
                        Toast.LENGTH_SHORT).show();
            } else {
                parseJson();
                FragmentManager fm = MainActivity.this
                        .getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                Layout1 fragment = new Layout1();

                ft.add(R.id.activity_main_content_fragment, fragment);
                ft.commit();

                if (dialog != null) {
                    dialog.dismiss();

                }
            }

        }

    }, 5000);

和Layout1部分代码

private ConnectionDetector cd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    callback = new Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            // the holder reference is already passed as arg0 here
            mMediaPlayer.setDisplay(holder);
            try {
                // progressDialog.dismiss();
                mMediaPlayer.start();
                mcontroller.setEnabled(true);

            } catch (IllegalStateException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {

        }
    };

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout1, null);
    SurfaceView v = (SurfaceView) view
            .findViewById(R.id.screen_tutorial_video_surface);
    popap = (RelativeLayout) view.findViewById(R.id.popaplayout);
    Typeface tf2 = Typeface.createFromAsset(getActivity().getAssets(),
            fontPath1);
    poptext = (TextView) view.findViewById(R.id.popaptext);
    poptext.setText(readFileFromAssets());
    poptext.setTypeface(tf2);
    poptext.setLineSpacing(2, 1.2f);
    cd = new ConnectionDetector(getActivity());

    close_popap = (ImageView) view.findViewById(R.id.closepopap);
    close_popap.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            popap.setVisibility(View.INVISIBLE);

        }
    });
    v.getHolder().addCallback(callback);


    handler = new Handler();

    if (!cd.isConnectingToInternet()) {

    } else {
        playVideo();
    }

    // parseJson();
    return view;
}

private void playVideo() {
    mcontroller = new MediaController(getActivity());
    // mcontroller.setVisibility(View.VISIBLE);
    mcontroller.setEnabled(true);
    mMediaPlayer = MediaPlayer.create(getActivity(),
            Uri.parse(Global.yutubeDownloadUrl));
    mMediaPlayer.setOnBufferingUpdateListener(this);
    mMediaPlayer.setOnCompletionListener(this);
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setScreenOnWhilePlaying(true);
    mMediaPlayer.setOnVideoSizeChangedListener(this);

    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    // START VIDEO PLAING TO RANDOM

    // START VIDEO PLAING TO RANDOM
}

@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    // TODO Auto-generated method stub
}

@Override
public void onPrepared(MediaPlayer mp) {

}

@Override
public void onCompletion(MediaPlayer mp) {

}

P.S 我的应用程序工作Wifi连接完美,但在3G连接应用crushed.i不知道我做错了什么

0 个答案:

没有答案