0

背景情報: チャイルド ロック アプリケーションを作成したいと考えています。Homeしたがって、ユーザーがアプリケーションにいるときは、すべてのキー (キーを含む) を無効にする必要があります。私は基本的にこれに似たアプリケーションが欲しいです。

問題: 別のアプリケーションをデフォルトのホーム ランチャーとして設定した場合 (つまり、HTC ではホーム センス UI、または Samsung の携帯電話で同様のもの)、自分のアプリケーションをデフォルトのホーム ランチャーとして設定した場合、Homeキーを押すと、ホーム画面 (アプリケーションをデフォルトのホーム ランチャーとして設定していますが!)。ここで、他のアプリケーションをデフォルトのホームランチャーとして設定せず、自分のアプリケーションのみを設定しても問題はありません。アプリケーション内でHomeキーを押しても、アプリケーションにとどまります。

アプリケーションをデフォルトとして設定する前にデフォルトのホーム アプリケーションを設定すると、Homeキーが機能しない (つまり、アプリケーションから離れてしまう) のはなぜですか? ただし、アプリケーションをデフォルトとしてのみ設定すると、Homeキーが機能します (つまり、アプリに残ります)。

以下は、私のテスト アプリケーションのサンプル コードです。

AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testspinner.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.java:

public class MainActivity extends Activity {

    private PackageManager pm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.pm = getPackageManager();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public boolean isHomeActivity() {
        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();

        packageManager.getPreferredActivities(filters, activities, null);

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

    protected void onResume() {
        super.onResume();
        if (!isHomeActivity()) {
            Intent localIntent = new Intent(Intent.ACTION_MAIN);
            localIntent.addCategory(Intent.CATEGORY_HOME);
            localIntent.setComponent(new ComponentName("android",
                    "com.android.internal.app.ResolverActivity"));
            startActivity(localIntent);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK: {
            finish();
        }
        }
        return false;
    }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
4

2 に答える 2

0

このプロジェクトはGithubで確認できます:https ://github.com/Neamar/Summon

これは、適切に機能する代替のホームアプリケーションです。

何があなたの説明された行動を生み出すことができるのかわかりません。これはおそらくデバイス固有の問題です。

アプリを設定した後、再起動を試みましたか?

于 2013-03-05T02:59:44.060 に答える
0

Androidの特定のバージョンを使用している場合、実際にはデフォルトとして設定されているアプリのデフォルトをクリアする必要があります. 私はあなたと同じ問題を抱えていました。次に、アプリ マーケットで Home Switcher for Froyo というアプリをチェックしました。そのアプリでデフォルトが設定されている場合、Home Switcher はデフォルト ランチャーのアプリ設定画面を表示し、ユーザーに「デフォルトをクリアする」ボタンを選択するように指示します。これは私が知っている唯一の方法です。

したがって、アプリでは、いくつかの異なる点を確認する必要があります。

  1. デフォルトのランチャーが設定されていない場合は、あなたのものを設定してください(上記のように、すでに機能しています)

  2. デフォルトが設定されている場合は、そのアプリ設定を起動し、ユーザーに「デフォルトのクリア」を選択するように求め、ホームボタンを押してアプリを選択してもらいます

これがお役に立てば幸いです。私のアプリにはまだ少しバグがあるため、コード例を求めるのに最適な人物であるかどうかはわかりませんが、スタック オーバーフローの回答をいくつか紹介できます。

https://stackoverflow.com/a/4772481/1817139

https://stackoverflow.com/a/10344985/1817139

頑張ってください。行き詰まったらメールを送ってください (lus u s_v [i] r at yahoo) - 括弧とスペースは無視してください

于 2013-03-05T05:15:35.360 に答える