Android 按下小部件按钮后如何启动活动?

Android 按下小部件按钮后如何启动活动?,android,android-widget,android-activity,Android,Android Widget,Android Activity,根据教程,我快速创建了一个简单的小部件,在主屏幕上显示当前时间: 我修改了代码,使小部件还带有一个按钮。我的目标是在按下按钮后启动一项活动。 不幸的是,我真的不明白在哪里听按钮点击。这是否必须进入清单文件的广播接收器部分 <!-- Broadcast Receiver --> <receiver android:name=".WifiSSIDWidget" android:label="@string/app_name"> <intent-filter&

根据教程,我快速创建了一个简单的小部件,在主屏幕上显示当前时间:

我修改了代码,使小部件还带有一个按钮。我的目标是在按下按钮后启动一项活动。 不幸的是,我真的不明白在哪里听按钮点击。这是否必须进入清单文件的广播接收器部分

<!--  Broadcast Receiver -->
<receiver android:name=".WifiSSIDWidget" android:label="@string/app_name">
    <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
    </intent-filter>
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/wifi_ssid_widget_provider" />
</receiver>    
单击按钮后,我希望调用以下代码:

public void openWifiSettings() {
    final Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}   
在更新中,将以下内容添加到按钮:

Intent intent = new Intent(ACTION_BUTTON1_CLICKED);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button_1, pendingIntent);
在AppWidget的
onReceive

@Override
public void onReceive(Context context, Intent intent)
{
 Log.d(TAG, "onReceive() " + intent.getAction());
 super.onReceive(context, intent);
...
if (ACTION_BUTTON1_CLICKED.equals(intent.getAction()))
{
 // your code
}
还要将您的意图添加到清单中

<intent-filter>
   <action android:name="com.example.myapp.BUTTON1_CLICKED" />
   ....

....

我应该在哪里定义单击的
操作按钮1
<代码>挂起内容也无法解析。您可以详细介绍一下您的解决方案吗?
remoteview.setonclickpendingent(R.id.button_1,pendingent)
在更新的
方法中给了我一个错误:
无法解决
。确切的错误消息是什么?例如,当然,您必须将id为
button_1
的按钮添加到您的小部件布局中。
无法解析RemoteView
。我想这是因为我在公共MyTime中创建了
RemoteView
。我应该如何更改代码以使其仍能工作?在调用
appWidgetManager.updateAppWidget
之前,尝试将这些行放入
run
方法中。也许您必须创建
remoteview
final:
final remoteview-remoteview
@Override
public void onReceive(Context context, Intent intent)
{
 Log.d(TAG, "onReceive() " + intent.getAction());
 super.onReceive(context, intent);
...
if (ACTION_BUTTON1_CLICKED.equals(intent.getAction()))
{
 // your code
}
<intent-filter>
   <action android:name="com.example.myapp.BUTTON1_CLICKED" />
   ....