在按钮上单击隐藏应用程序并通过拨号器取消隐藏

时间:2015-04-16 05:08:13

标签: android

我想在按钮点击时隐藏我在Android中的应用程序,并通过拨打特定号码取消隐藏它(比如拨打12345将取消隐藏它)。

我正在使用Android Studio 1.1.0。目标API级别为21。

我已经尝试过stackoverflow上的每一个代码。

请建议我一种方式或工作代码......

隐藏我使用的图标:

PackageManager pm = getPackageManager();
ComponentName componentName = new ComponentName(this,
        com.redski.hideunhide.MainActivity.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP);

我做的其他功能是:

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 *#55555*");
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
        }
        });
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.show();
}

要通过拨号程序取消隐藏,我创建了一个LaunchViaDialReceiver类,如下所示:

package com.redski.hideunhide;

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

public class LaunchViaDialReceiver extends BroadcastReceiver {

    private static final String LAUNCHER_NUMBER = "*#55555*";
    @Override
    public void onReceive(Context context, Intent intent) {
        //TODO Auto-generated method stub
        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);
        }
    }
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.redski.hideunhide" >

    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>



        <receiver android:name=".LaunchViaDialReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

0 个答案:

没有答案