删除活动作为默认启动器

时间:2015-02-10 07:21:40

标签: android android-intent

我正在尝试删除我作为默认启动器的活动。我跟着这个link但是得到了错误。以下是我的代码和错误:

lockScreenAppActivity

    @Override
    protected void onResume() {
        ComponentName componentName = new ComponentName(LockScreenAppActivity.this,LockScreenAppActivity.class);
        if (!isMyLauncherDefault()) {
            Log.e("heloooo", "MyActivity is not default home activity!");

            // toggle fake activity
            PackageManager pm = getPackageManager();
            int flag = ((pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
            pm.setComponentEnabledSetting(componentName, flag, PackageManager.DONT_KILL_APP);

            // start home activity to enable chooser
            Intent selector = new Intent(Intent.ACTION_MAIN);
            selector.addCategory(Intent.CATEGORY_HOME);
            startActivity(selector);
        }
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_POWER) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_CAMERA)) {
            //this is where I can do my stuff
            return true; //because I handled the event
        }
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {

            return true;
        }

        return false;

    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_POWER || (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) || (event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
            //Intent i = new Intent(this, NewActivity.class);
            //startActivity(i);
            return false;
        }
        if ((event.getKeyCode() == KeyEvent.KEYCODE_HOME)) {

            System.out.println("alokkkkkkkkkkkkkkkkk");
            return true;
        }
        return false;
    }

    boolean isMyLauncherDefault() {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
        filter.addCategory(Intent.CATEGORY_HOME);

        List<IntentFilter> filters = new ArrayList<IntentFilter>();
        filters.add(filter);

        final String myPackageName = getPackageName();
        List<ComponentName> activities = new ArrayList<ComponentName>();
        final PackageManager packageManager = (PackageManager) getPackageManager();

        // You can use name of your package here as third argument
        packageManager.getPreferredActivities(filters, activities, null);

        for (ComponentName activity : activities) {
            if (myPackageName.equals(activity.getPackageName())) {
                return true;
            }
        }
        return false;
    }

}

错误

  

启动应用程序:   com.example.home.lockscreenapp / com.example.home.lockscreenapp.LockScreenAppActivity。       DEVICE SHELL COMMAND:我开始-n“com.example.home.lockscreenapp / com.example.home.lockscreenapp.LockScreenAppActivity”   -a android.intent.action.MAIN -c android.intent.category.LAUNCHER       警告:链接器:libvc1dec_sa.ca7.so具有文本重定位。这会浪费内存并且存在安全风险。请修理。       警告:链接器:libvc1dec_sa.ca7.so具有文本重定位。这会浪费内存并且存在安全风险。请修理。       开始:意图{act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER]   cmp = com.example.home.lockscreenapp / .LockScreenAppActivity}       错误类型3       错误:活动类{com.example.home.lockscreenapp / com.example.home.lockscreenapp.LockScreenAppActivity}   不存在。

menifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.home.lockscreenapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <android:uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="18" />
    <android:uses-permission
        android:name="android.permission.READ_EXTERNAL_STORAGE"
        android:maxSdkVersion="18" />

    <application
        android:icon="@drawable/lockicon"
        android:label="@string/app_name" >
        <activity
            android:name=".LockScreenAppActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <!-- The following two intent-filters are the key to set homescreen -->
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".StartLockScreen"
            android:theme="@style/Theme.Transparent" >

            <!--
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />



            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>
            -->
        </activity>

        <service android:name=".MyService" >
        </service>

        <receiver
            android:name="reciever.lockScreenReeiver"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

尝试清除packagePreferences getPackageManager().clearPackagePreferredActivities(getPackageName());