Reprint Instructions
This article may have been updated, the latest article please go: http://www.sollyu.com/android-code-snippets/
Description
This article for the personal daily use of a collation of this code snippet, this article Wenzheng will be updated occasionally
Code Evaluation Application
activity.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" + activity.getPackageName())));
Get a list of system shares
/** * Get system Share list * @param context * @return */public static list< ResolveInfo > Getshareapps (Context context) {I Ntent Intent = new Intent (intent.action_send, NULL); Intent.addcategory (Intent.category_default); Intent.settype ("*/*"); Packagemanager Pmanager = Context.getpackagemanager (); Return Pmanager.queryintentactivities (Intent, packagemanager.component_enabled_state_default);//for (int i = 0 ; I < resolveinfos.size (); i++)//{//Appinfovo Appinfovo = new Appinfovo ();//ResolveInfo ResolveInfo = Resolveinfos. Get (i);//Appinfovo.setappname (Resolveinfo.loadlabel (Packagemanager). toString ());//Appinf Ovo.seticon (Resolveinfo.loadicon (Packagemanager));//Appinfovo.setpackagename (Resolveinfo.activityinfo. PackageName);//Appinfovo.setlaunchername (resolveInfo.activityInfo.name);//Appinfovos.add (app Infovo);//}//return APPinfovos;}
Get Current IP Address
HttpUtils.GetHtml ( "http://1111.ip138.com/ic.asp", null, new HttpUtils.HttpUtilsCallBack (){ @Override public void OnFinish ( HttpResponse httpResponse, int resultCode, String resultString ) { Pattern p = Pattern.compile ( "\\[(.+)\\]" ); Matcher m = p.matcher ( resultString ); if ( m.find () ) { String ipAddress = m.group ( 1 ); LogUtils.OutputDebugString ( ipAddress ); } } @Override public void OnError ( Exception e ) { LogUtils.OutputDebugString ( e ); }} );
Get the root view of the current activity
/** * 获得当前Activity的根视图 * @param activity * @return */public static ViewGroup GetContentView(Activity activity){ return ( ViewGroup ) ( ( ViewGroup ) activity.findViewById ( android.R.id.content ) ).getChildAt ( 0 );}
Open the App
public static void Openapp (context context, String Packagename_) {try {Intent resolveintent = new Inte NT (intent.action_main, NULL); Resolveintent.addcategory (Intent.category_launcher); Resolveintent.setpackage (Context.getpackagemanager (). Getpackageinfo (packagename_, 0). PackageName); List<resolveinfo> apps = Context.getpackagemanager (). Queryintentactivities (resolveintent, 0); ResolveInfo ri = Apps.iterator (). Next (); if (ri = null) {String packagename = ri.activityInfo.packageName; String className = ri.activityInfo.name; Intent Intent = new Intent (intent.action_main); Intent.addcategory (Intent.category_launcher); ComponentName cn = New ComponentName (PackageName, className); Intent.setcomponent (CN); Context.startactivity (Intent); }} catch (Packagemanager.namenotfoundexception e) {e.printstacktrace (); }}
Open URL
public static void OpenUrl(Context context, String url){ android.content.Intent intent = new android.content.Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(android.net.Uri.parse(url)); context.startActivity(intent);}public static void OpenUrl(Context context, int url){ android.content.Intent intent = new android.content.Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(android.net.Uri.parse(context.getString(url))); context.startActivity(intent);}
Create a desktop shortcut
public static void CreateShortcut(Context context, String appName, Class<?> startClass, int icon){ Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName); shortcut.putExtra("duplicate", false);// 设置是否重复创建 Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(context, startClass);// 设置第一个页面 shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, icon); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); context.sendBroadcast(shortcut);}
Share pictures to a circle of friends
public static void shareMultiplePictureToTimeLine ( Context context, File... files ){ Intent intent = new Intent (); ComponentName comp = new ComponentName ( "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI" ); intent.setComponent ( comp ); intent.setAction ( Intent.ACTION_SEND_MULTIPLE ); intent.setType ( "image/*" ); ArrayList< Uri > imageUris = new ArrayList< Uri > (); for ( File f : files ) { imageUris.add ( Uri.fromFile ( f ) ); } intent.putParcelableArrayListExtra ( Intent.EXTRA_STREAM, imageUris ); context.startActivity ( intent );}
Status bar Transparency
/** * 设置状态栏透明 * @param activity */public static void TranslucentStatus(Activity activity){ if ( android.os.Build.VERSION.SDK_INT > 18 ) { Window window = activity.getWindow (); window.setFlags ( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS , WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS ); window.setFlags ( WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION ); }}
Progressdialogloading
public static void ProgressDialogLoading ( final Context context, final ProgressDialogLoadingCallBack progressDialogLoadCallBack ){ final ProgressDialog progressDialog = new ProgressDialog ( context ); progressDialogLoadCallBack.onInit ( context, progressDialog ); new Thread ( new Runnable () { @Override public void run () { progressDialogLoadCallBack.onRun ( context, progressDialog ); progressDialog.dismiss (); } } ).start ();}public static interface ProgressDialogLoadingCallBack{ public void onInit ( Context context, ProgressDialog progressDialog ); public void onRun ( Context context, ProgressDialog progressDialog );}
ImageView Setting up pictures
ImageView.setImageResource(R.drawable.icon);
Kill the corresponding Android program
/** * 杀死对应的Android程序,而不会自动启动 * @param pkgName 应用程序的包名 */public static void forceStopAPK ( String pkgName ) throws Exception{ Process sh = Runtime.getRuntime ().exec ( "su" ); DataOutputStream os = new DataOutputStream ( sh.getOutputStream () ); final String Command = "am force-stop " + pkgName + "\n"; os.writeBytes ( Command ); os.flush (); sh.waitFor ();}
Note
Android-code Snippets