package com.work.driver.utils;
import java.io.File;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import com.library.app.instrument.LogOut;
import com.work.driver.R;
/**
* 工具类
*
* @author xugang
*
*/
public class PhoneUtils {
/**
* 启动APP
*
* @param context
* @param pkg
*/
public void startApp(Context context, String pkg) {
try {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkg);
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
LogOut.e("APP Error:" + e.getMessage());
}
}
/**
* 判断APK是否安装
*
* @param context
* @param uri
* @return
*/
public boolean isAppInstalled(Context context, String uri) {
PackageManager pm = context.getPackageManager();
boolean installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
installed = true;
} catch (PackageManager.NameNotFoundException e) {
installed = false;
}
return installed;
}
/**
* 发通知
*
* @param context
* @param intent
* @param title
* @param content
*/
public void sendNotification(Context context, Intent intent, String title, String content) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(content);
mBuilder.setWhen(System.currentTimeMillis());
PendingIntent pendingintent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingintent);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_ALL;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify((int) System.currentTimeMillis(), notification);
}
/**
* 判断当前应用程序处于前台还是后台
*/
public boolean isApplicationBroughtToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
/**
* 判断指定包名的进程是否运行
*
* @param context
* @param packageName
* 指定包名
* @return 是否运行
*/
public boolean isRunning(Context context, String packageName) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> infos = am.getRunningAppProcesses();
for (RunningAppProcessInfo rapi : infos) {
if (rapi.processName.equals(packageName))
return true;
}
return false;
}
/**
* 安装APK
*
* @param uri
* @param activity
*/
public void installAPK(String uri, Context activity) {
File file = new File(uri);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
// intent.addFlags(Intent.flag_ac);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
@SuppressLint("DefaultLocale")
public String getNetState(Context ctx){
String nettype = ""; // 联网方式
try{
ConnectivityManager manager = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null)
nettype = info.getTypeName().toUpperCase();// getTypeName返回一个网络的名字,比如wifi;toUpperCase把字符串变成大写
if (nettype.indexOf("WIFI") < 0) {
nettype = (manager.getAllNetworkInfo()[0].getSubtypeName() != null ? manager
.getAllNetworkInfo()[0].getSubtypeName().toUpperCase()
: nettype);
}
nettype = nettype.split(" ")[0];
}catch (Exception e) {
}
return nettype;
}
/**
* 获得设备的IMEI号
* @param context 参数
* @return String 返回类型
* */
public String getIMEI(Context ctx)
{
String imei = "";
TelephonyManager telephonyManager = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null)
imei = telephonyManager.getDeviceId();
if (TextUtils.isEmpty(imei))
imei = "0";
return imei;
}
/**
*
* @Title getIMSI
* @Description 返回唯一的用户ID;就是这张卡的唯一编号
* @param context 参数
* @return String 返回类型
*/
public String getIMSI(Context ctx)
{
String imsi = "";
TelephonyManager telephonyManager = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
if (imsi != null)
{
imsi = telephonyManager.getSubscriberId();
}
if(TextUtils.isEmpty(imsi))
imsi = "0";
return imsi;
}
/**
* 获得mac地址
*
* @param context 参数
* @return String 返回类型
* */
public String getMac(Context ctx){
String mac = "";
WifiManager wifi = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE);
if(wifi!=null){
WifiInfo info = wifi.getConnectionInfo();
if(info!=null)
mac = info.getMacAddress();
}
if(TextUtils.isEmpty(mac)){
mac = "0";
}
return mac;
}
/**
* 获得设备上的手机号码
*
* @param context 参数
* @return String 返回类型
* */
public String getPhoneNumber(Context ctx)
{
String number = "";
TelephonyManager telephonyManager = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null)
number = telephonyManager.getLine1Number();
if (TextUtils.isEmpty(number))
number = "0";
return number;
}
/**
* Role:Telecom service providers获取手机服务商信息 <BR>
* 需要加入权限<uses-permission
* android:name="android.permission.READ_PHONE_STATE"/> <BR>
* Date:2012-3-12 <BR>
*
* @param Context
* @return String
*/
public String getProvidersName(Context context) {
String ProvidersName = null;
// 返回唯一的用户ID;就是这张卡的编号神马的
String IMSI = getIMSI(context);
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中国移动";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中国联通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中国电信";
}
return ProvidersName;
}
/**
* 获取应用版本号
*
* @param ctx
* @return
*/
public String getSoftwareVersion(Context ctx) {
String version = "1.0";
try { // PackageInfo:通过manifast收集关于这个包得所有信息
PackageInfo packageInfo = ctx.getPackageManager().getPackageInfo(
ctx.getPackageName(), 0);
version = packageInfo.versionName;
} catch (Exception e) {
LogOut.e(e.toString());
}
return version;
}
/**
* 获取应用版本号
*
* @param ctx
* @return
*/
public int getSoftwareVersionCode(Context ctx) {
int version = 1;
try { // PackageInfo:通过manifast收集关于这个包得所有信息
PackageInfo packageInfo = ctx.getPackageManager().getPackageInfo(
ctx.getPackageName(), 0);
version = packageInfo.versionCode;
} catch (Exception e) {
LogOut.e(e.toString());
}
return version;
}
/**
* @Title getScreenWidth
* @Description 屏幕的宽
* @param activity
* @author aceway-liwei
* @date 2013-12-29 下午03:56:07
*/
@SuppressWarnings("deprecation")
public int getScreenWidth(Activity activity){
Display display = activity.getWindowManager().getDefaultDisplay(); // 获得屏幕显示
return display.getWidth();
}
/**
* @Title getScreenWidth
* @Description 屏幕的高
* @param activity
* @author aceway-liwei
* @date 2013-12-29 下午03:56:07
*/
@SuppressWarnings("deprecation")
public int getScreenHeight(Activity activity){
Display display = activity.getWindowManager().getDefaultDisplay(); // 获得屏幕显示
return display.getHeight();
}
/**
* @Title getScreenWidth
* @Description 密度
* @param activity
* @author aceway-liwei
* @date 2013-12-29 下午03:56:07
*/
public float getDensity(Activity activity){
DisplayMetrics mDisplayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(
mDisplayMetrics);
return mDisplayMetrics.density;
}
/**
* 发短信接口 show = true 调出编辑短信界面,show = false 直接发送短信
*
* @<uses-permission
* android:name="android.permission.SEND_SMS"></uses-permission
* >
* @param context
* @param number
* @param msg
* @param show
*
*/
public void SendMessage(Context context, String number, String msg,
boolean show) {
if (show) {
Uri uri1 = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri1);
i.putExtra("sms_body", msg);
context.startActivity(i);
} else {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg, null, null);
}
}
/**
* 播放音乐接口 example path = "file:///sdcard/song.mp3"
*
* @param context
* @param filepath
*/
public void PlayMedia(Context context, String filepath) {
Uri uri = Uri.parse(filepath);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setType("audio/mp3");
context.startActivity(it);
}
/**
* 播放视频接口 example path = "file:///sdcard/media.mp4"
*
* @param context
* @param filepath
*/
public void PlayVideo(Context context, String filepath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse(filepath);
intent.setDataAndType(uri, "video/*");
context.startActivity(intent);
}
/**
* 打开浏览器 并打开网页 example ur = "http://www.baidu.com"
*
* @<uses-permission android:name="android.permission.INTERNET" />
* @param context
* @param url
*/
public void OpenBrowser(Context context, String url) {
Uri uri = Uri.parse(url); // 浏览器
Intent it = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(it);
}
/**
* 返回Intent 类似桌面点击icon进入应用
* @param activity 包名 +"/" +.XXXX.XXXXActivity 主activity
* @return
*/
public Intent getIntent(String activity){
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setComponent(ComponentName.unflattenFromString(activity));
return intent;
}
/**
* 从assets得到渠道ID(chid)
*
* @param buffer
* @return 存储结果 longal 2012-05-23
*/
public String readAssetsChId(Context context) {
String chid = "";
try {
chid = readInputStream(context.getResources().getAssets().open(CHANNEL_FILENAME));
} catch (Exception e) {
e.printStackTrace();
return "0";
}
return chid;
}
/**
* @Title getConfigParams
* @Description 通过Key 得到配置文档里的参数
* @param context
* @param key
* @return 参数
* @return String 返回类型
*/
public String getConfigParams(Context context, String key) {
ApplicationInfo appi = null;
String value;
try {
appi = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = appi.metaData;
if (bundle != null) {
value = bundle.getString(key);
if (value != null)
return value;
}
throw new RuntimeException("请检查" + key + " 在Manifest文件中是否配置正确");
} catch (Exception e) {
// e.printStackTrace();
}
return "";
}
/**
* 打电话
*
* @param mContext
*/
public void showCall(Activity mContext, String phone) {
Intent intent;
intent = new Intent(Intent.ACTION_DIAL);
Uri url = Uri.parse("tel:" + phone);
intent.setData(url);
mContext.startActivity(intent);
}
}
Android 手机常用方法
最新推荐文章于 2019-06-10 13:37:06 发布