我修改了代码,以便小部件也带有一个按钮.我的目标是在按下按钮后启动一个活动.
不幸的是,我真的不明白在哪里听按钮点击.这是否必须进入清单文件的广播接收器部分?
<!-- 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>
或者代码是否进入onUpdate?这是Widget类代码:
public class HelloWidget extends appwidgetprovider { @OverrIDe public voID onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIDs) { Timer timer = new Timer(); timer.scheduleAtFixedrate(new MyTime(context,appWidgetManager),1,1000); } private class MyTime extends TimerTask { RemoteVIEws remoteVIEws; AppWidgetManager appWidgetManager; Componentname thisWidget; DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,Locale.getDefault()); public MyTime(Context context,AppWidgetManager appWidgetManager) { this.appWidgetManager = appWidgetManager; remoteVIEws = new RemoteVIEws(context.getPackagename(),R.layout.main); thisWidget = new Componentname(context,HelloWidget.class); } @OverrIDe public voID run() { remoteVIEws.setTextVIEwText(R.ID.Widget_textvIEw,"TIME = " +format.format(new Date())); appWidgetManager.updateAppWidget(thisWidget,remoteVIEws); } } }
这是我想在单击按钮后调用的代码:
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);}解决方法
import androID.app.PendingIntent;...public static final String ACTION_button1_CliCKED = "com.example.myapp.button1_CliCKED";
在onUpdate中将以下内容添加到按钮:
Intent intent = new Intent(ACTION_button1_CliCKED);PendingIntent pendingIntent = PendingIntent.getbroadcast(context,intent,0);remoteVIEws.setonClickPendingIntent(R.ID.button_1,pendingIntent);
并在onReceive你的AppWidget
@OverrIDepublic 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" /> ....总结
以上是内存溢出为你收集整理的android – 如何在按下widget按钮后启动活动?全部内容,希望文章能够帮你解决android – 如何在按下widget按钮后启动活动?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)