1
可能重复:
How to launch activity from android home screen widget我如何启动一个活动与家庭的Widget
请告诉我最简单的代码来启动从应用的主屏幕小部件的活动?
我到目前为止的代码不工作,没有在LogCat中显示.. ??
控件内有一个图像,当图像被点击时,应该启动应用程序。寻找一个片段。
public class MyWidget extends AppWidgetProvider
{
// Create an Intent to launch activity from ImageView
@Override
public void onEnabled(Context context)
{
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName comp = new ComponentName(context.getPackageName(),MyWidget.class.getName());
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent myPI = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.content);//<-THE LAYOUT W/I THE MainActivity CLASS??
views.setOnClickPendingIntent(R.id.widgetLauncher, myPI);//<-THE IMAGEVIEW ID??
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
mgr.updateAppWidget(comp, views);
}
}
控件布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/widgetLauncher"
android:layout_width="258dp"
android:layout_height="54dp"
android:layout_centerInParent="true"
android:src="@drawable/wgt_logo" >
</ImageView>
</RelativeLayout>
http://buildmobile.com/how-to-code-an-android-widget – slayton 2012-04-05 17:15:23
@Colin皮卡德一直在尝试各种方式,但没有。查看上面添加的代码 – CelticParser 2012-04-05 18:00:45
什么是负号?我已经完成了研究,并且我无法得到任何工作。我添加了更多的代码。无论人们。 – CelticParser 2012-04-05 18:02:12