我正在做一个Android应用程序。我想在模拟器中隐藏应用程序图标,我想通过按一些数字来启动我的应用程序,例如456#。有没有办法做到这一点?
答案 0 :(得分:19)
要以编程方式从启动器隐藏应用程序图标,您可以执行此操作
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context,
LauncherActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
按数字启动应用程序 首先在mainfest文件中添加以下权限
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
然后注册接收器
<receiver android:name=".LaunchAppViaDialReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
然后创建一个接收器类
public class LaunchAppViaDialReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
if (null == bundle)
return;
String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//here change the number to your desired number
if (phoneNubmer.equals("12345")) {
setResultData(null);
Gaurdian.changeStealthMode(context,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
Intent appIntent = new Intent(context, LauncherActivity.class);
appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(appIntent);
}
}
答案 1 :(得分:14)
如果您想隐藏应用图标,最好先显示图标,让用户知道图标消失后如何启动应用。首先在清单中创建一个活动别名,然后在那里移动您的intent过滤器。这样,您可以在不禁用活动的情况下禁用图标。
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity-alias
android:name=".Launcher"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
使用您的包名称获取启动器别名的组件名称:
private static final ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
"your.package.name", "your.package.name.Launcher");
您可以检查它是否已被禁用...
private boolean isLauncherIconVisible() {
int enabledSetting = getPackageManager()
.getComponentEnabledSetting(LAUNCHER_COMPONENT_NAME);
return enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
}
...并在提供用户信息后在适当时禁用它:
private void hideLauncherIcon() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Important!");
builder.setMessage("To launch the app again, dial phone number 12345.");
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();
}
要从拨号器启动,请创建广播接收器:
public class LaunchViaDialReceiver extends BroadcastReceiver {
private static final String LAUNCHER_NUMBER = "12345";
@Override
public void onReceive(Context context, Intent intent) {
String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (LAUNCHER_NUMBER.equals(phoneNubmer)) {
setResultData(null);
Intent appIntent = new Intent(context, MainActivity.class);
appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(appIntent);
}
}
}
将其添加到清单:
<receiver android:name=".LaunchViaDialReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
并向清单添加权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
答案 2 :(得分:5)
问题第一部分的答案,请尝试以下代码:
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
您的应用程序将不可见,但用户仍可在设置&gt;&gt;中找到它。应用&gt;&gt;管理申请
This answer也可能对您有所帮助。
如果您已经实现了功能(按下某个号码并打开我们的应用程序),请不要忘记在此处发布您的答案。
答案 3 :(得分:4)
请注意解决方案:
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
将使应用程序无法从Google Play升级,因为操作系统在此组件禁用后无法找到该程序包并且无法重新安装它,除非该应用程序未安装manullay(这不是用户友好的行为)
答案 4 :(得分:2)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hideapplication();
}
private void hideapplication() {
// TODO Auto-generated method stub
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
}