Android联网方式判断
Java代码
1. package com.amaker.net;
2.
3. import android.app.Activity;
4. import android.content.Context;
5. import android.content.Intent;
6. import android.database.Cursor;
7. import android.net.ConnectivityManager;
8. import android.net.NetworkInfo;
9. import android.net.NetworkInfo.State;
10. import android.net.Uri;
11. import android.os.Bundle;
12. import android.widget.TextView;
13.
14. /**
15. * @Title: JudgeNewWayDemoActivity.java
16. * @Package com.xiaoma.www
17. * @Description: 判断联网方式测试类
18. * @author MZH
19. */
20. public class JudgeNewWayDemoActivity extends Activity {
21.
22. private TextView text;
23.
24. /** Called when the activity is first created. */
25. @Override
26. public void onCreate(Bundle savedInstanceState) {
27. super.onCreate(savedInstanceState);
28. setContentView(R.layout.main);
29.
30. /**
31. * 下面列举几个可直接跳到联网设置的意图,供大家学习
32. */
33. //startActivity(new Intent(android.provider.Settings.ACTION_APN_SETTINGS));
34. //startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
35.
36. //用下面两种方式设置网络
37. //startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
38. //startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
39. init();
40. }
41.
42. /**
43. * 初始化实现
44. */
45. private void init(){
46. text = (TextView)findViewById(R.id.waytext);
47. /**获得系统级联网管理员对象*/
48. ConnectivityManager manager = (ConnectivityManager)this.getSystemService(CONNECTIVITY_SERVICE);
49. NetworkInfo info = manager.getActiveNetworkInfo();
50. if(info == null){ //无网情况下
51. //跳转到网络设置页面
52. startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
53. }else{ //有网情况下
54. if(info.isAvailable()){ //网络可用时
55. /**是手机自带的联网方式*/
56. if(info.getType() == ConnectivityManager.TYPE_MOBILE){
57. if(info.getState() == State.CONNECTED){
58. text.setText(String.valueOf("手机网络可用并已连接"+"\n"+"连接网络方式为:\n"+info.getType()+",MOBILE"));
59. }
60.
61. /**WIFI联网方式*/
62. }else{
63. text.setText(String.valueOf("连接网络方式为:"+info.getType()+",WI-FI"));
64. startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
65. }
66. }else{
67. text.setText(String.valueOf("手机网络不可用"));
68. }
69.
70. }
71. }
72.
73. /**
74. * 临时写来练练的方法,大家可以跳过
75. */
76. private void kkk(){
77. Uri uri = Uri.parse("content://telephony/carriers");
78. Cursor cr = getContentResolver().query(uri, null, null, null, null);
79. while(cr!=null && cr.moveToNext()){
80. // APN id
81. String id = cr.getString(cr.getColumnIndex("_id"));
82. // APN name
83. String apn =
补充:移动开发 , Android ,