Android隐藏Activity

基本原理是:在运行或者显示Activity时,如果需要隐藏时,将Activity跳转到Home主页面就可以,就可以"虚假的"隐藏了Activity了,下面是Demo:

package com.example.androidhideactivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private final static String TAG="MainActivity";
    private Button mButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mButton=(Button)findViewById(R.id.button1);
        mButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                
                //隐藏activity到后台,打开HOME Launcher  
                PackageManager pm = 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));  
                startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
                try {    
                startActivity(startIntent);  
                    } catch (ActivityNotFoundException e) {  
                        Log.i(TAG,"not found Activity error="+e.getMessage());  
                        } catch (SecurityException e) {  
                            Log.i(TAG,"not found Activity error="+e.getMessage());  
                        Log.e(TAG,"Launcher does not have the permission to launch "+ startIntent  
                        + ". Make sure to create a MAIN intent-filter for the corresponding activity "+ "or use the exported attribute for this activity.",  
                                        e);  
                } 
                
                
            }
            
        });
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

转载于:https://www.cnblogs.com/MMLoveMeMM/articles/4047279.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值