欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android开发小技巧汇总

程序员文章站 2022-06-04 15:22:25
...

1.Android根据包名打开应用

public void startAPP(String appPackageName){
  try{
   Intent intent = this.getPackageManager().getLaunchIntentForPackage(appPackageName);
   startActivity(intent);
  }catch(Exception e){
   Toast.makeText(this, "没有安装", Toast.LENGTH_LONG).show();
  }
 } 

 

2.Android根据包名判断是否安装某个应用

public static boolean checkApkExist(Context context, String packageName) {
  if (packageName == null || "".equals(packageName))
   return false;
  try {
   ApplicationInfo info = context.getPackageManager()
     .getApplicationInfo(packageName,
       PackageManager.GET_UNINSTALLED_PACKAGES);
   return true;
  } catch (NameNotFoundException e) {
   return false;
  }
 }

3.Android检查是否有网络访问

public static boolean isNetworkAvaliable(Context context) {
  // 获取所有连接管理对象
  ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  if (null != cm) {
   // 获取网络连接管理的对象
   NetworkInfo info = cm.getActiveNetworkInfo();
   if (null != info && info.isConnected()) {
    // 判断当前网络是否已经连接
    if (NetworkInfo.State.CONNECTED == info.getState()) {
     return true;
    }
   }
  }
  return false;
 }

 

4.Android获取设置下面帐户信息

Account[] accounts = AccountManager.get(this).getAccounts();
  for(Account account : accounts){
   LogUtil.e("tag", account.name+","+account.type+","+account.describeContents());
  }

 

记得添加权限:<uses-permission android:name="android.permission.GET_ACCOUNTS" />

 

5.监听ListView滑动到底部

listview.setOnScrollListener(new OnScrollListener(){ 
    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState){ 
        // 当不滚动时 
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { 
            // 判断是否滚动到底部 
            if (view.getLastVisiblePosition() == view.getCount() - 1) { 
                //加载更多功能的代码 
            } 
        } 
    } 
});