Android - 코드 세 션

7472 단어 android
전재 설명
이 글 은 이미 업데이트 되 었 을 수 있 습 니 다. 최신 글 은 다음 과 같 습 니 다. http://www.sollyu.com/android-code-snippets/
설명 하 다.
이 글 은 개인 이 일상적으로 사용 할 수 있 도록 정 리 된 이 코드 세 션 입 니 다. 이 글 은 정 해진 시간 에 업데이트 되 지 않 습 니 다.
코드
평가 응용
activity.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" + activity.getPackageName())));

시스템 공유 목록 가 져 오기
/**
 *         
 * @param context
 * @return
 */
public static List< ResolveInfo > getShareApps ( Context context )
{
    Intent 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 () );
//            appInfoVo.setIcon ( resolveInfo.loadIcon ( packageManager ) );
//            appInfoVo.setPackageName ( resolveInfo.activityInfo.packageName );
//            appInfoVo.setLauncherName ( resolveInfo.activityInfo.name );
//            appInfoVos.add ( appInfoVo );
//        }
//        return appInfoVos;
}

현재 IP 주소 가 져 오기
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 );
    }
} );

현재 Activity 의 루트 보기 가 져 오기
/**
 *     Activity    
 * @param activity
 * @return
 */
public static ViewGroup GetContentView(Activity activity)
{
    return ( ViewGroup ) ( ( ViewGroup ) activity.findViewById ( android.R.id.content ) ).getChildAt ( 0 );
}

응용 프로그램 열기
public static void OpenApp(Context context, String packageName_)
{
    try
    {
        Intent resolveIntent = new Intent(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();
    }
}

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);
}

데스크 톱 바로 가기 만 들 기
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);
}

위 챗 모멘트
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 );
}

상태 표시 줄 투명
/**
 *        
 * @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 설정 그림
ImageView.setImageResource(R.drawable.icon);

대응 하 는 Android 프로그램 죽 이기
/**
 *      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 + "
"; os.writeBytes ( Command ); os.flush (); sh.waitFor (); }

비고

좋은 웹페이지 즐겨찾기