码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发中常用到方法总结

时间:2014-12-08 17:34:41      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   http   io   ar   color   os   sp   

1.判断服务是否在运行中
 public static boolean isServiceRunning(Context context, String serviceName) {
  boolean isRunning = false;
  ActivityManager activityManager = (ActivityManager) context
    .getSystemService(Context.ACTIVITY_SERVICE);
  List<ActivityManager.RunningServiceInfo> serviceList = activityManager
    .getRunningServices(Integer.MAX_VALUE);
  if (!(serviceList.size() > 0)) {
   return false;
  }
  for (int i = 0; i < serviceList.size(); i++) {
   RunningServiceInfo si = (RunningServiceInfo) serviceList.get(i);
   if (si.service.getClassName().equals(serviceName)) {
    isRunning = true;
    break;
   }
  }
  return isRunning;
 }

2.判断存储卡是否已插入
 public static boolean isExternalStorageAvailable() {
  String state = Environment.getExternalStorageState();
  if (Environment.MEDIA_MOUNTED.equals(state)) {
   return true;
  } else {
   return false;
  }
 }

3.获得该文件的路径
 public static String getSDCardPath(String folderName) {
  if(folderName!=null){
   String path = Environment.getExternalStorageDirectory()
   .getAbsolutePath() + File.separator + folderName;
   File file = new File(path);
   if (!file.exists()) {
    file.mkdir();
   }
   return path;
  }else{
   return null;
  }
 }

4.获得手机IMEI
 public static String getIMEI(Context ctx) {
  if(ctx!=null){
   String imei = "";
   TelephonyManager telephonyManager = (TelephonyManager) ctx
     .getSystemService(Context.TELEPHONY_SERVICE);
   if (telephonyManager != null)
    imei = telephonyManager.getDeviceId();
   if (imei == "")
    imei = "0";
   return imei;
  }else{
   return null;
  }
 }

5.获得手机号
 public static String getPhoneNumber(Context ctx) {
  if(ctx!=null){
   String number = "";
   TelephonyManager telephonyManager = (TelephonyManager) ctx
   .getSystemService(Context.TELEPHONY_SERVICE);
   if (telephonyManager != null)
    number = telephonyManager.getLine1Number();
   if (number == "")
    number = "0";
   return number;
  }else{
   return null;
  }
 }

6.得到手机状态信息
 public static String fetch_status(Context ctx) {
  TelephonyManager tm = (TelephonyManager) ctx
    .getSystemService(Context.TELEPHONY_SERVICE);//

  String str = "";
  str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
  str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion()
    + "\n";
  str += "Line1Number = " + tm.getLine1Number() + "\n";
  str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
  str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";
  str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";
  str += "NetworkType = " + tm.getNetworkType() + "\n";
  str += "honeType = " + tm.getPhoneType() + "\n";
  str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";
  str += "SimOperator = " + tm.getSimOperator() + "\n";
  str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";
  str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";
  str += "SimState = " + tm.getSimState() + "\n";
  str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";
  str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";
  return str;
 }

7.得到软件版本信息
 public static String getSoftwareVersion(Context ctx) {
  String version = "";
  try {
   PackageInfo packageInfo = ctx.getPackageManager().getPackageInfo(
     ctx.getPackageName(), 0);
   version = packageInfo.versionName;
  } catch (PackageManager.NameNotFoundException e) {
   e.printStackTrace();
  }
  return version;
 }

8.得到当前日期
 public static String getCurrDate(String format) {
  String str_format = (format == null || format == "") ? "yyyy-MM-dd HH:mm:ss"
    : format;
  Date date = new Date();
  SimpleDateFormat from = new SimpleDateFormat(str_format);
  String times = from.format(date);
  return times;
 }

9.根据URL获取bitmap
 public static Bitmap getURLBitmap(String uriPic) throws Exception {
  URL imageUrl = null;
  Bitmap bitmap = null;
  try {
   imageUrl = new URL(uriPic);
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
  try {
   HttpURLConnection conn = (HttpURLConnection) imageUrl
     .openConnection();
   conn.setConnectTimeout(5*1000);
      conn.setReadTimeout(5*1000);
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setUseCaches(true);
      conn.setRequestMethod("POST");
   conn.connect();
   InputStream is = conn.getInputStream();
   bitmap = BitmapFactory.decodeStream(is);
   is.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return bitmap;
 }

10.隐藏app
 public static void HiddenApp(Context ctx) {
  PackageManager pm = ctx.getPackageManager();
  ResolveInfo homeInfo = pm.resolveActivity(
    new Intent(Intent.ACTION_MAIN)
      .addCategory(Intent.CATEGORY_HOME), 0);
  ActivityInfo ai = homeInfo.activityInfo;
  Intent startIntent = new Intent(Intent.ACTION_MAIN);
  startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
  startIntent.setComponent(new ComponentName(ai.packageName, ai.name));
  ctx.startActivity(startIntent);
 }

11.退出程序(3种方法)
 public static void ExitApp(Context ctx) {


  ActivityManager am = (ActivityManager)
  ctx.getSystemService(Context.ACTIVITY_SERVICE);
  am.restartPackage(ctx.getPackageName());
  
  Intent startMain = new Intent(Intent.ACTION_MAIN);
  startMain.addCategory(Intent.CATEGORY_HOME);
  startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  ctx.startActivity(startMain);
  android.os.Process.killProcess(android.os.Process.myPid());
  System.exit(0);
  
  android.os.Process.killProcess(android.os.Process.myPid());
 }

12.查询手机中所有非系统应用

 

 public static List<PackageInfo> getAllApps(Context context) {

 List<PackageInfo> apps = new ArrayList<PackageInfo>();

 PackageManager pManager = context.getPackageManager();

 //获取手机内所有应用

 List<PackageInfo> paklist = pManager.getInstalledPackages(0);

 for (int i = 0; i < paklist.size(); i++) {

 PackageInfo pak = (PackageInfo) paklist.get(i);

 //判断是否为非系统预装的应用程序

 if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {

 // customs applications

 apps.add(pak);

 }

 }

 return apps;

 }

 13.查询手机内所有支持分享的应用


 public static List<ResolveInfo> getShareApps(Context context)
 {
  List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
  Intent intent = new Intent(Intent.ACTION_SEND, null);
  intent.addCategory(Intent.CATEGORY_DEFAULT);
  intent.setType("text/plain");
  PackageManager pManager = context.getPackageManager();
  mApps = pManager
    .queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
  return mApps;
 }

 14.获取 Andorid 手机WIFI连接的Mac地址和IP地址

  public static String getInfo()

    {

       WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

       WifiInfo info = wifi.getConnectionInfo();

 

       String maxText = info.getMacAddress();

       String ipText = intToIp(info.getIpAddress());

       String status = "";

       if (wifi.getWifiState() == WifiManager.WIFI_STATE_ENABLED)

       {

           status = "WIFI_STATE_ENABLED";

       }

       String ssid = info.getSSID();

       int networkID = info.getNetworkId();

       int speed = info.getLinkSpeed();

       return "mac:" + maxText + "\n\r"

              + "ip:" + ipText + "\n\r"

              + "wifi status :" + status + "\n\r"

              + "ssid :" + ssid + "\n\r"

              + "net work id :" + networkID + "\n\r"

              + "connection speed:" + speed + "\n\r"

              ;

    }

15.获取手机上所有非系统应用

public static ArrayList<AppInfo> getInstalledPackages(Context context)

{

ArrayList<AppInfo> appList = new ArrayList<AppInfo>();

List<PackageInfo> packages = context.getPackageManager()

.getInstalledPackages(0);

 

for (int i = 0; i < packages.size(); i++)

{

PackageInfo packageInfo = packages.get(i);

AppInfo tmpInfo = new AppInfo();

tmpInfo.appName = packageInfo.applicationInfo.loadLabel(

context.getPackageManager()).toString();

tmpInfo.packageName = packageInfo.packageName;

tmpInfo.versionName = packageInfo.versionName;

tmpInfo.versionCode = packageInfo.versionCode;

tmpInfo.appIcon = packageInfo.applicationInfo

.loadIcon(context.getPackageManager());

if((packageInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0)

       {

//如果非系统应用,则添加至appList

           appList.add(tmpInfo);

       }

}

return appList;

}

16.将流转成字节数组

public byte[] stream2Bytes(InputStream is) throws IOException {

byte[] mByte;

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int length = 0;

while ((length = is.read(buffer, 0, 1024)) != -1) {

baos.write(buffer, 0, length);

}

baos.flush();

mByte = baos.toByteArray();

baos.close();

return mByte;

}

结伴旅游网www.jieberu.com

领取免费门票就上推推族www.tuituizu.com

Android开发中常用到方法总结

标签:des   android   style   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/taleche/p/4151183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!