2013-10-01 152 views
35

我下面的代碼已經用於隱藏應用程序圖標編程Android的隱藏/取消隱藏應用程序圖標編程

try{ 
    PackageManager p = getPackageManager(); 
    p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 
}catch (Exception e) { 
    e.printStackTrace(); 
} 

現在,我想使圖標可見編程

回答

5

只需使用下面的代碼。

PackageManager p = getPackageManager(); 
ComponentName componentName = new ComponentName("com.example.removeicon","com.example.removeicon.LauncherActivity"); 
p.setComponentEnabledSetting(componentName , PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 

請注意,圖標可能不會消失,直到下一次重新啓動。使用下面的代碼

+0

我已經使用該代碼隱藏應用程序圖標,現在我想看到該應用程序圖標 – PankajAndroid

+1

看到我更新的答案。 –

+0

看到我更新的答案可能會幫助你。 –

77

隱藏應用程序的圖標:

PackageManager p = getPackageManager(); 
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" /> 
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 

這裏是如何帶回了應用程序的圖標。

PackageManager p = getPackageManager(); 
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class); 
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 
+2

你好,你的代碼工作完美,但我有一個棘手的事情去做。隱藏圖標後,如果用戶撥打#007等特定號碼,我想啓動應用程序。我已經實現了傳出呼叫接收器和匹配號碼我試圖開始我的主要活動,但它給我ActivityNotFoundException。你能幫助我,如果你有任何想法.. – Scorpion

+0

@Scorpion是的,你是正確的,將摧毀你的活動,你不能訪問該活動。因爲你需要用另一種方式。 – CoronaPintu

+0

你可以建議我該怎麼做?我得到了:我的代碼的startActivity行上的java.lang.NullPointerException – Scorpion

9

要隱藏圖標使用此:

PackageManager p = getPackageManager(); 
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class); 
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 

,並取消隱藏圖標:

PackageManager p = getPackageManager(); 
ComponentName componentName = new ComponentName(this, com.apps.MainActivity.class); 
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 

重要: 這在某種程度上棘手的,如果你需要做的主要活動東西隱藏您的應用。你將面臨一個ActivityNotFoundException。爲了使其工作,您應該在做任何事情之前取消隱藏圖標,然後在完成後再次隱藏它。
簡單的步驟:在這裏獲得
2,取消隱藏圖標
3推出的主要活動
4 - 做你的事情上的主要活動
5隱藏圖標再次

3

下載源代碼 1通話從這裏(Hide and Unhide the app icon in android programmatically

MainActivity.java:

package com.deepshikha.hideappicon; 

import android.Manifest; 
import android.app.ProgressDialog; 
import android.content.ComponentName; 
import android.content.DialogInterface; 
import android.content.pm.PackageManager; 
import android.os.Handler; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    Button btn_hide; 
    private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
      "com.deepshikha.hideappicon", "com.deepshikha.hideappicon.Launcher"); 

    public static int REQUEST_PERMISSIONS = 1; 
    boolean boolean_permission; 
    ProgressDialog progressDialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     init(); 
     fn_permission(); 
     listener(); 
    } 

    private void init() { 
     btn_hide = (Button) findViewById(R.id.btn_hide); 
     progressDialog = new ProgressDialog(MainActivity.this); 
     progressDialog.setTitle("Alert"); 
     progressDialog.setMessage("Please wait"); 


     if (isLauncherIconVisible()) { 
      btn_hide.setText("Hide"); 
     } else { 
      btn_hide.setText("Unhide"); 
     } 


    } 

    private void listener() { 
     btn_hide.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.btn_hide: 

       progressDialog.show(); 
       new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         progressDialog.dismiss(); 
         if (isLauncherIconVisible()) { 
          btn_hide.setText("Hide"); 
         } else { 
          btn_hide.setText("Unhide"); 
         } 
        } 
       }, 10000); 


       if (boolean_permission) { 

        if (isLauncherIconVisible()) { 
         fn_hideicon(); 
        } else { 
         fn_unhide(); 
        } 
       } else { 
        Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show(); 
       } 
       break; 

     } 

    } 

    private boolean isLauncherIconVisible() { 
     int enabledSetting = getPackageManager().getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME); 
     return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 
    } 

    private void fn_hideicon() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Important!"); 
     builder.setMessage("To launch the app again, dial phone number 1234567890"); 
     builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       getPackageManager().setComponentEnabledSetting(LAUNCHER_COMPONENT_NAME, 
         PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
         PackageManager.DONT_KILL_APP); 
      } 
     }); 
     builder.setIcon(android.R.drawable.ic_dialog_alert); 
     builder.show(); 
    } 

    private void fn_unhide() { 
     PackageManager p = getPackageManager(); 
     ComponentName componentName = new ComponentName(this, com.deepshikha.hideappicon.MainActivity.class); 
     p.setComponentEnabledSetting(LAUNCHER_COMPONENT_NAME, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 
    } 

    private void fn_permission() { 
     if ((ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED) || 
       (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED)) { 

      if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.PROCESS_OUTGOING_CALLS))) { 
      } else { 
       ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.PROCESS_OUTGOING_CALLS}, 
         REQUEST_PERMISSIONS); 

      } 

      if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.PROCESS_OUTGOING_CALLS))) { 
      } else { 
       ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS}, 
         REQUEST_PERMISSIONS); 

      } 
     } else { 
      boolean_permission = true; 


     } 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     if (requestCode == REQUEST_PERMISSIONS) { 

      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       boolean_permission = true; 


      } else { 
       Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show(); 

      } 
     } 
    } 
} 

LaunchAppReceiver.java:

package com.deepshikha.hideappicon; 

import android.content.BroadcastReceiver; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 

/** 
* Created by deepshikha on 9/6/17. 
*/ 

public class LaunchAppReceiver extends BroadcastReceiver { 
    String LAUNCHER_NUMBER = "1234567890"; 
    private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
      "com.deepshikha.hideappicon", "com.deepshikha.hideappicon.Launcher"); 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
     if (LAUNCHER_NUMBER.equals(phoneNubmer)) { 
      setResultData(null); 

      if (isLauncherIconVisible(context)) { 

      } else { 
       Intent appIntent = new Intent(context, MainActivity.class); 
       appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(appIntent); 
      } 


     } 

    } 

    private boolean isLauncherIconVisible(Context context) { 
     int enabledSetting = context.getPackageManager().getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME); 
     return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 
    } 

} 

謝謝!