当前位置: 编程技术>移动开发
本页文章导读:
▪判断网络部类及是否有网络 判断网络类型及是否有网络
判断当前是否有可用网络
public boolean checkNetworkAvailable() {
boolean isNetworkAvailable = false;
android.net.ConnectivityManager connManager = (android.net.ConnectivityManager)getApplicationContex.........
▪ 几种创办LayoutInflater对象的方法 几种创建LayoutInflater对象的方法
LayoutInflater layoutInflater;
1.layoutInflater = Context.getLayoutInflater();
2.layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3.layoutInflater = LayoutInflater.........
▪ Service初始 Service初步
例子:ServiceActivity.classpublic class ServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInsta.........
[1]判断网络部类及是否有网络
来源: 互联网 发布时间: 2014-02-18
判断网络类型及是否有网络
判断当前是否有可用网络
public boolean checkNetworkAvailable() { boolean isNetworkAvailable = false; android.net.ConnectivityManager connManager = (android.net.ConnectivityManager)getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE); if(connManager.getActiveNetworkInfo() != null){ isNetworkAvailable = connManager.getActiveNetworkInfo().isAvailable(); } return isNetworkAvailable; }
判断当前网络类型
public String getNetworkType() { android.net.ConnectivityManager connManager = (android.net.ConnectivityManager)getApplicationContext().getSystemService(android.content.Context.CONNECTIVITY_SERVICE); android.net.NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); if(state == State.CONNECTED || state == State.CONNECTING){ return "wifi"; } //3G网络判断 state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); if(state == State.CONNECTED || state == State.CONNECTING){ return "mobile"; } return "none"; }
[2] 几种创办LayoutInflater对象的方法
来源: 互联网 发布时间: 2014-02-18
几种创建LayoutInflater对象的方法
LayoutInflater layoutInflater;
1.layoutInflater = Context.getLayoutInflater();
2.layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
3.layoutInflater = LayoutInflater.from(this);
1 楼
CoolinZ
2012-06-12
第三中似乎最常用
[3] Service初始
来源: 互联网 发布时间: 2014-02-18
Service初步
例子:
ServiceActivity.class
FirstService.class
AndroidManifest.xml
PS:如果到了版本android2.1以上,将使用onStartCommand方法替代onStart方法。
例子:
ServiceActivity.class
public class ServiceActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button1 = (Button)findViewById(R.id.button1); Button button2 = (Button)findViewById(R.id.button2); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.setClass(ServiceActivity.this, FirstService.class); startService(i); } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(); i.setClass(ServiceActivity.this, FirstService.class); stopService(i); } }); } }
FirstService.class
public class FirstService extends Service { @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub System.out.println("service onBind"); return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println("service onCreate"); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("service onDestroy"); } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); System.out.println("service onStart"); System.out.println("startId:"+startId); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="4" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServiceActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".FirstService"></service> </application> </manifest>
PS:如果到了版本android2.1以上,将使用onStartCommand方法替代onStart方法。
最新技术文章: