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

时间:2025-01-28 07:51:18

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

}

public class MyReceiver extends BroadcastReceiver {

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

}

public class MyService extends Service {

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

<ScrollView xmlns:andro
    xmlns:tools="/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="" >

    <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:andro
    package=""
    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="" />

                <category android:name="" />
            </intent-filter>
        </activity>
        <receiver android:name="">
            <intent-filter >
                <action android:name=""/>
            </intent-filter>
        </receiver>
        <service android:name=""></service>
    </application>

</manifest>