19

作成した時計ウィジェットで問題が発生しています。ユーザーに時計をタッチして、スマートフォンで時計アプリを起動してもらいたい。これはコードです:

//this worked on my nexus 2.1 
if(VERSION.SDK.equals("7")){
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

            Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.deskclock", "com.android.deskclock.DeskClock"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
            views.setOnClickPendingIntent(R.id.Widget, pendingIntent);

            AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);

        }
  //this worked on my nexus +froyo2.2           
  else if(VERSION.SDK.equals("8")){
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

            Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.google.android.deskclock", "com.android.deskclock.DeskClock"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
            views.setOnClickPendingIntent(R.id.Widget, pendingIntent);

            AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);

        }
 //this worked on my htc magic with 1.5 and 1.6
        else{
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

            Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setComponent(new ComponentName("com.android.alarmclock", "com.android.alarmclock.AlarmClock"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, AlarmClockIntent, 0);
            views.setOnClickPendingIntent(R.id.Widget, pendingIntent);

            AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
        }

上記を作成したので、時計をタッチするとアラーム設定が開きます。しかし、普遍的ではありません。2.2のドロイドでは動作しないことがわかりました。世界中のすべてのAndroidフォンフレーバーに対してifステートメントを作成するよりも優れたソリューションが必要です。さらに、私はすべてのパッケージ名を知りません。誰もがこれを克服する方法を知っています助けてください。

4

8 に答える 8

42

このコードは私の時計ウィジェットで機能します。

PackageManager packageManager = context.getPackageManager();
Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);

// Verify clock implementation
String clockImpls[][] = {
        {"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
        {"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
        {"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
        {"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock",  "com.motorola.blur.alarmclock.AlarmClock"},
        {"Samsung Galaxy Clock", "com.sec.android.app.clockpackage","com.sec.android.app.clockpackage.ClockPackage"} ,
        {"Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" },
        {"ASUS Tablets", "com.asus.deskclock", "com.asus.deskclock.DeskClock"}

};

boolean foundClockImpl = false;

for(int i=0; i<clockImpls.length; i++) {
    String vendor = clockImpls[i][0];
    String packageName = clockImpls[i][1];
    String className = clockImpls[i][2];
    try {
        ComponentName cn = new ComponentName(packageName, className);
        ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
        alarmClockIntent.setComponent(cn);
        debug("Found " + vendor + " --> " + packageName + "/" + className);
        foundClockImpl = true;
    } catch (NameNotFoundException e) {
        debug(vendor + " does not exists");
    }
}

if (foundClockImpl) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, alarmClockIntent, 0);
        // add pending intent to your component
        // ....
}

このようにして、デフォルトのクロックマネージャーを実行できます。

于 2010-11-25T22:22:58.633 に答える
18

Mayraの回答をフォローアップするために、クラスandroid.provider.AlarmClockにはこれに適しIntentたものがありますが、APIレベル9以上でのみ使用できます。(少なくともGoogleの誰かがこのようなリクエストを聞いています!)

ウィジェットから時計アプリを起動するには、次のコードが機能します。

Intent openClockIntent = new Intent(AlarmClock.ACTION_SET_ALARM);
openClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openClockIntent);

次の権限も必要です。
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

于 2011-03-04T15:54:38.873 に答える
6

APIレベル19から、これを使用してデフォルトの時計アプリにアラームリストを表示できます。

    Intent mClockIntent = new Intent(AlarmClock.ACTION_SHOW_ALARMS);
    mClockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(mClockIntent);
于 2015-03-13T14:57:38.283 に答える
4

アプリケーションの外部でアクティビティを開始する正しい方法は、暗黙的なインテントを使用することです。暗黙のインテントでは、開始する特定のクラスを指定せず、代わりに、他のアプリが実行することを選択できるアクションを指定します。

これは、他のアプリの作成者がクラスの名前を変更することなどを決定したときに、アプリが破損するのを防ぐためです。また、複数のアプリが同じアクションに応答できるようにします。

暗黙のインテントの詳細については、インテントとインテントフィルターを参照してください。

残念ながら、これを機能させるには、アプリ開発者が特定のアクションをすべて実装することに同意する必要があります。時計アプリにこれがない場合、あなたが望むことを達成するための良い方法はありません。

sdkのバージョンが異なるだけでなく、電話メーカーがアプリの置き換えを選択できるため、電話が異なるとアプリが異なる可能性があるため、あなたのような巨大なifステートメントを作成しようとしないことをお勧めします。したがって、時計アプリを開くことができることに依存している場合、アプリは常に新しい電話で壊れることになります。

于 2010-08-28T17:02:03.773 に答える
2

コード:

            Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
            i.putExtra(AlarmClock.EXTRA_MESSAGE, "Alarm Title");
            ArrayList<Integer> alarmDays = new ArrayList<>();
            alarmDays.add(Calendar.SATURDAY);
            alarmDays.add(Calendar.SUNDAY);
            alarmDays.add(Calendar.MONDAY);
            alarmDays.add(Calendar.TUESDAY);
            alarmDays.add(Calendar.WEDNESDAY);
            alarmDays.add(Calendar.THURSDAY);
            alarmDays.add(Calendar.FRIDAY);
            i.putExtra(AlarmClock.EXTRA_DAYS, alarmDays);
            i.putExtra(AlarmClock.EXTRA_VIBRATE, true);
            i.putExtra(AlarmClock.EXTRA_HOUR, 10);
            i.putExtra(AlarmClock.EXTRA_MINUTES, 21);
            startActivity(i);

許可:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
于 2017-07-11T10:24:20.373 に答える
1

@Mayraは正しいです。それが方法です。ただし、ネイティブクロックアプリケーションにはインテントが登録されておらず、インテントフィルターを使用していません。システム環境設定以外に、それを開くネイティブアプリケーションはないと思います。

また、ネイティブクロックアプリケーションは、同じ形式では利用できないか、別の電話ではまったく利用できません。これは、SDKのバージョン管理を超えています。したがって、どちらかをオンにする(良い)方法は実際にはありません。

最善の方法は、source.android.comにパッチを送信して、時計アプリケーションにインテントフィルターを適用してから、http: //www.openintents.org/en/intentstableにインテントを登録することです。少なくとも、アプリはデバイスのサブセットで動作し、他のデバイスでは正常に失敗します(インテントを処理するアプリケーションが見つからないと言っています)。

于 2010-09-26T18:07:32.440 に答える
1

フルッソの答えのための小さな追加。Google Galaxy Nexus電話で目覚まし時計を起動するには:

{"Standar Alarm Clock2", "com.google.android.deskclock", "com.android.deskclock.AlarmClock"},

それ以外の場合は、目覚まし時計の代わりに時計アプリケーションが起動されます

于 2012-08-26T14:03:17.900 に答える
-1

あなたは付け加えられます

{ "Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" }

新しいXperia電話をサポートするためにfrussoの答えに。

于 2013-11-23T21:15:45.497 に答える