Intent to launch the clock application on android

2019-01-04 10:03发布

I am facing a problem with a clock widget i made. I want the user to touch the clock and launch the clock app on the phone. this is the code:

//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);
        }

I made the above so the when i touch the clock opens the alarm settings. But is not universal. I found out that on droids with 2.2 does not work. There must be a better solution than creating an if statement for every android phone flavor i the world. Plus i don not know the package names for all. Anyone knows how to overcome this please help.

8条回答
唯我独甜
2楼-- · 2019-01-04 10:41

Code:

            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);

Permission:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
查看更多
Deceive 欺骗
3楼-- · 2019-01-04 10:42

The correct way to start an activity outside of your application is to use a Implicit intent. With an implicit intent you do not provide a specific class to start, instead you specify an action that other apps can choose to act upon.

This is to prevent breakage in your app when the creator of the other app decides to rename their class, or some such. Also, it allows multiple apps to respond to the same action.

See intents and intent filters for more details on implicit intents.

Unfortunately, you need to app developers to agree to all implement a particular action in order for this to work. If the clock apps don't have this, then there is no good way to accomplish what you want.

I would suggest against trying to create a huge if statement like you have, since the app could be different not only with different sdk versions, but with different phones since the phone manufacturers can choose to replace apps. Thus, your app is going to be constantly breaking on new phones, if you rely on being able to open the clock app.

查看更多
登录 后发表回答