如何从android中的小部件打开应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3589741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to open a application from widget in android?
提问by RMK
when we click the widget at that time i need to open a activity screen(or application).How to do this?
当我们当时点击小部件时,我需要打开一个活动屏幕(或应用程序)。如何做到这一点?
回答by DeRagan
You need to set an onClickpendingIntent on your widget
您需要在小部件上设置 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);
Check this out
看一下这个
回答by Shivanand Darur
Include this code in yout WidgetProvider class's onUpdate() method.
将此代码包含在 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();
}
}
回答by DonSteep
The Android developer pages for App Widgets has information and a full example doing exactly this: http://developer.android.com/guide/topics/appwidgets/index.html
App Widgets 的 Android 开发者页面包含信息和完整示例:http: //developer.android.com/guide/topics/appwidgets/index.html
回答by Hitesh Sahu
Using Kotlin
使用 Kotlin
You need to add PendingIntent
on Click of your widget Views
您需要添加PendingIntent
点击您的小部件视图
remoteViews.setOnClickPendingIntent(R.id.widgetRoot,
PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0))
Where widgetRootis the id of my widget's parent ViewGroup
其中widgetRoot是我的小部件父级的 IDViewGroup
In On Update
更新中
Pending intent is usually added in onUpdatecallback
Pending Intent 通常添加在onUpdate回调中
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray) {
// There may be multiple widgets active, so update all of them
val widgetIds = appWidgetManager.getAppWidgetIds( ComponentName(context, ClockWidget::class.java))
for (appWidgetId in widgetIds) {
// Construct the RemoteViews object
val remoteViews = RemoteViews(context.packageName, R.layout.clock_widget)
//Open App on Widget Click
remoteViews.setOnClickPendingIntent(R.id.weatherRoot,
PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0))
//Update Widget
remoteViews.setTextViewText(R.id.appWidgetText, Date().toString())
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}