Подтвердить что ты не робот

Как открыть приложение из виджета в Android?

когда мы нажимаем на виджет в это время, мне нужно открыть экран активности (или приложение). Как это сделать?

4b9b3361

Ответ 1

Вам нужно установить onClickpendingIntent на ваш виджет

Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);

Проверьте это

Обработайте более одного нажатия кнопки на Android Widget

Ответ 2

Включите этот код в свой метод WidgetProvider onUpdate().

for(int j = 0; j < appWidgetIds.length; j++) 
{
    int appWidgetId = appWidgetIds[j];

    try {
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");

        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        intent.setComponent(new ComponentName("your Application package",
            "fully qualified name of main activity of the app"));
        PendingIntent pendingIntent = PendingIntent.getActivity(
            context, 0, intent, 0);
        RemoteViews views = new RemoteViews(context.getPackageName(),
            layout id);
        views.setOnClickPendingIntent(view Id on which onclick to be handled, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, views);
    } catch (ActivityNotFoundException e) {
            Toast.makeText(context.getApplicationContext(),
                    "There was a problem loading the application: ",
                    Toast.LENGTH_SHORT).show();
    }

}