实现四大组件的禁用和启用,包括隐藏app桌面图标

这里只演示了Activity,BroadcastReceiver,Service的禁用和启用,没有演示ContentProvider,道理都一样。


/**
 * @author Administrator
 * 通过PackageManager的setComponentEnabledSetting()方法,实现四大组件(Activity,BroadcastReceiver,Service,ContentProvider)的禁用和启用.
 * 在PackageManager中定义的四大组件的三种基本状态有:
 * COMPONENT_ENABLED_STATE_ENABLED:组件可用
 * COMPONENT_ENABLED_STATE_DISABLED:组件不可用
 * COMPONENT_ENABLED_STATE_DEFAULT:默认,即你清单文件中组件的默认状态。
 * 可通过PackageManager的getComponentEnabledSetting(ComponentName componentName)方法获取组件的当前状态;
 * 可通过PackageManager的setComponentEnabledSetting(ComponentName componentName,int newState, int flags)方法设置组件的状态
 * 需要注意的是,当设置app的LAUNCHER Activity的状态为COMPONENT_ENABLED_STATE_DISABLED时,即为隐藏该app在桌面的图标,但是在设置>application选项里
 * 仍然可看到该activity存在
 */
public class MainActivity extends FragmentActivity {
	private PackageManager pm;
	private ComponentName activity;
	private ComponentName receiver;
	private ComponentName service;
	private int activity_state;
	private int receiver_state;
	private int service_state;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		pm = getPackageManager();
		activity = new ComponentName(this, MainActivity.class);
		receiver = new ComponentName(this,MyReceiver.class);
		service = new ComponentName(this, MyService.class);
	}
	/**
	 * 发送远程广播
	 * @param v
	 */
	public void sendFar(View v){
		Intent intent = new Intent();
		intent.setAction("com.intent.diy");
		sendBroadcast(intent);
	}
	/**
	 * 启动MyService服务
	 */
	public void start(View v){
		Intent intent = new Intent(this, MyService.class);
		startService(intent);
	}
	/**
	 * 停止MyService服务
	 */
	public void stop(View v){
		Intent intent = new Intent(this,MyService.class);
		stopService(intent);
	}
	//----------------------------------设置MyReceiver的状态---------------------------------------
	//设置MyReceiver的状态为Default
	public void setReceiverZero(View v){
		pm.setComponentEnabledSetting(receiver, 
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
	}
	//设置MyReceiver的状态为Enabled
	public void setReceiverOne(View v){
		pm.setComponentEnabledSetting(receiver, 
				PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
	}
	//设置MyReceiver的状态为Disabled,禁用广播接收者
	public void setReceiverTwo(View v){
		pm.setComponentEnabledSetting(receiver, 
				PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
	}
	
	//----------------------------------设置MyService的状态---------------------------------------
	
	//设置MyService的状态为Default
	public void setServiceZero(View v){
		pm.setComponentEnabledSetting(service, 
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
	}
	//设置MyService的状态为Enabled
	public void setServiceOne(View v){
		pm.setComponentEnabledSetting(service, 
				PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
	}
	//设置MyService的状态为Disabled,禁用服务,不可被启动,不可被绑定等
	public void setServiceTwo(View v){
		pm.setComponentEnabledSetting(service, 
				PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
	}
	
	//----------------------------------设置MainActivity的状态---------------------------------------
	
	//设置MainActivity的状态为Default
	public void setActivityZero(View v){
		pm.setComponentEnabledSetting(activity, 
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
	}
	//设置MainActivity的状态为Enabled
	public void setActivityOne(View v){
		pm.setComponentEnabledSetting(activity, 
				PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
	}
	//设置MainActivity的状态为Disabled,会隐藏应用的桌面图标
	public void setActivityTwo(View v){
		pm.setComponentEnabledSetting(activity, 
				PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
	}
	
	/**
	 * 获取组件的状态
	 */
	public void getState(View v){
		//获取MainActivity组件的状态
		activity_state = pm.getComponentEnabledSetting(activity);
		System.out.println("MainActivity的state:"+activity_state);
		//获取MyReceiver组件的状态
		receiver_state = pm.getComponentEnabledSetting(receiver);
		System.out.println("MyReceiver的state:"+receiver_state);
		//获取MyService组件的状态
		service_state = pm.getComponentEnabledSetting(service);
		System.out.println("MyService的state:"+service_state);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}

public class MyReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("MyReceiver接收到广播");
	}

}

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("执行MyService的onBind");
		return new MiddlePerson();
	}
	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("执行MyService的onUnbind");
		return super.onUnbind(intent);
	}
	@Override
	public void onCreate() {
		System.out.println("执行MyService的onCreate");
		super.onCreate();
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("执行MyService的onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	@Override
	public void onDestroy() {
		System.out.println("执行MyService的onDestroy");
		super.onDestroy();
	}
	public void methodInService(){
		System.out.println("我是MyService内部的方法");
	}
	class MiddlePerson extends Binder{
		private void callMethodInService(){
			methodInService();
		}
	}
}

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.yin.localbroadcast.MainActivity" >

    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="getState"
        android:text="获取组件的状态"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendFar"
        android:text="向MyReceiver发送广播" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="启动MySevice服务" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stop"
        android:text="停止MyService服务" />
    <!-- MyReceiver的状态 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setReceiverZero"
        android:text="设置MyReceiver的状态为Default" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setReceiverOne"
        android:text="设置MyReceiver的状态为Enabled" />
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setReceiverTwo"
        android:text="设置MyReceiver的状态为Disabled" />
	<!-- 设置MyService的状态 -->
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setServiceZero"
        android:text="设置MyService的状态为Default" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setServiceOne"
        android:text="设置MyService的状态为Enabled" />
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setServiceTwo"
        android:text="设置MyService的状态为Disabled" />
	<!-- 设置MainActivity的状态 -->
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setActivityZero"
        android:text="设置MainActivity的状态为Default" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setActivityOne"
        android:text="设置MainActivity的状态为Enabled" />
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setActivityTwo"
        android:text="设置MainActivity的状态为Disabled,将隐藏桌面图标" />
</LinearLayout>
</ScrollView>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yin.localbroadcast"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.yin.localbroadcast.MyReceiver">
            <intent-filter >
                <action android:name="com.intent.diy"/>
            </intent-filter>
        </receiver>
        <service android:name="com.yin.localbroadcast.MyService"></service>
    </application>

</manifest>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值