2015-02-10 67 views
1

我試圖刪除我的活動作爲默認啓動器。我跟着這個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; 
    } 

} 

錯誤

Launching application: com.example.home.lockscreenapp/com.example.home.lockscreenapp.LockScreenAppActivity. DEVICE SHELL COMMAND: am start -n "com.example.home.lockscreenapp/com.example.home.lockscreenapp.LockScreenAppActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER WARNING: linker: libvc1dec_sa.ca7.so has text relocations. This is wasting memory and is a security risk. Please fix. WARNING: linker: libvc1dec_sa.ca7.so has text relocations. This is wasting memory and is a security risk. Please fix. Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.home.lockscreenapp/.LockScreenAppActivity } Error type 3 Error: Activity class {com.example.home.lockscreenapp/com.example.home.lockscreenapp.LockScreenAppActivity} does not exist.

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

試試清除軟件包首選項getPackageManager().clearPackagePreferredActivities(getPackageName());

相關問題