Android判断网络状态是否断开
http://hi.baidu.com/%C8%F0%F2%B6%CF%E9/blog/item/d40c4cd5b21edd0ea18bb7f7.html

Android 判断网络状态是否断开,不多说了,看代码吧!

/**

* 判断网络状态是否可用

* @return true: 网络可用 ; false: 网络不可用

*/

public boolean isConnectInternet() {

ConnectivityManager conManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE );

NetworkInfo networkInfo = conManager.getActiveNetworkInfo();

if (networkInfo != null ){ // 注意,这个判断一定要的哦,要不然会出错

return networkInfo.isAvailable();

}

return false ;

}

最后别忘要在加上

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

这个权限哦!

Android完全关闭应用程序
在工作过程序中遇到一个需要完全关闭应用程序的问题,在网络上找了一大堆的文章,每篇都是用 System.exit(0) 或者 android.os.Process.killProcess(android.os.Process.myPid()) 这两种方法,但是我试过了, System.exit(0) 这个根本不行,而 android.os.Process.killProcess(android.os.Process.myPid()) 这个只能关闭当前的 Activity ,也就是对于一个只有单个 Activity 的应用程序有效,如果对于有多外 Activity 的应用程序它就无能为力了。

下面我介绍一下对于多个 Activity 的应用程序的完全关闭方法:

ActivityManager 类中提供了如下的方法:

/**

* Have the system perform a force stop of everything associated with

* the given application package. All processes that share its uid

* will be killed, all services it has running stopped, all activities

* removed, etc. In addition, a {@link Intent#ACTION_PACKAGE_RESTARTED}

* broadcast will be sent, so that any of its registered alarms can * be stopped, notifications removed, etc.

*

* You must hold the permission * {@link android.Manifest.permission#RESTART_PACKAGES} to be able to

* call this method.

*

* @param packageName The name of the package to be stopped.

*/

public void restartPackage(String packageName) {

try {

ActivityManagerNative.getDefault().restartPackage(packageName);

}

catch (RemoteException e) { }

}

所以如果要关闭整个应用程序的话只需要运行以下两行代码就行:

ActivityManager activityMgr= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE );

activityMgr.restartPackage(getPackageName());

最后还需要添加这个权限才行:

<!-- 关闭应用程序的权限 -->
<uses-permission android:name="android.permission.RESTART_PACKAGES" />

本文讲述了Android中不同Activity之间的数据传递 — Bundle对象的实现(-)

描述

本文讲述了 Android 中不同 Activity 之间的数据传递 Bundle 对象的实现。

例子

有兴趣的朋友可以在登录后下载本文例子代码!

例子效果图

实现步骤

第一步 :建 Android 工程: BundleDemo

第二步 :编写 Activity 的子类别: BundleDemo ,其程序代码如下:

package com.a3gs.bundle;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioGroup;

public class BundleDemo extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button btn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

EditText et = (EditText) findViewById(R.id.height);

double height = Double.parseDouble(et.getText().toString());

String sex = "";

RadioGroup rg = (RadioGroup) findViewById(R.id.sex);

if(rg.getCheckedRadioButtonId() == R.id.M) {

sex = "M";

}

if(rg.getCheckedRadioButtonId() == R.id.F) {

sex = "F";

}

// Bundle 来绑定所要的数据

Bundle bd = new Bundle();

bd.putDouble("height", height);

bd.putString("sex", sex);

// 创建一个新的 Intent ,并将 Bundle 传进去

Intent it = new Intent();

it.putExtras(bd);

it.setClass(BundleDemo.this, Bundle2.class);

startActivity(it);

BundleDemo.this.finish();

}

});

}

}接下


更多相关文章

  1. Android(安卓)Wi-Fi工作原理
  2. android contentResolver的使用
  3. Android学习入门
  4. Android(安卓)安全架构及权限控制机制剖析
  5. android 网络访问-图片处理优秀开源项目
  6. 四极管:Android操作系统的结构
  7. 阅读《Android(安卓)从入门到精通》(1)——了解 Android(安卓)开发
  8. android重启
  9. Android的应用程序结构分析:HelloActivity

随机推荐

  1. ANDROID Bionic C DOCS
  2. android 4.0以上WebView不能全屏播放视频
  3. 【Android游戏开发二十五】在Android上的
  4. 一周一本技术书(第四周)《Android谷歌官方T
  5. android设计的网页工具+灵感
  6. 2019秋招android总结
  7. 【android】如何有效的减少重复的代码
  8. android第一周总结——但愿这次可以发出
  9. Android几种Service常驻内存的小思路
  10. Android——UI篇:Android(安卓)图片选择器