如何启动插件按钮后,活动pressed? [英] how to launch activity after widget button is pressed?

查看:118
本文介绍了如何启动插件按钮后,活动pressed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已经修改了code,这样的小工具还带有一个按钮。我的目标是推出一项活动,一旦按钮获取的pressed。 不幸的是,我真的不知道从哪里听来的按钮点击。这是否要进入广播接收器清单文件的部分?

 <! - 广播接收器 - >
<接收机器人:WifiSSIDWidget名称=机器人:标签=@字符串/ APP_NAME>
    <意向滤光器> <作用机器人:名称=android.appwidget.action.APPWIDGET_UPDATE/>
    &所述; /意图滤光器>
    <元数据的android:NAME =android.appwidget.provider安卓资源=@ XML / wifi_ssid_widget_provider/>
< /接收器>
 

抑或是code,而进入的onupdate ?这里的小部件类code:

 公共类HelloWidget的扩展AppWidgetProvider {

    @覆盖
    公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){

        定时器定时=新的Timer();
        timer.scheduleAtFixedRate(新数值指明MyTime(上下文,appWidgetManager),1,1000);
    }

    私有类数值指明MyTime扩展的TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        组件名thisWidget;
        日期格式FORMAT = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM,Locale.getDefault());

    公共数值指明MyTime(上下文的背景下,AppWidgetManager appWidgetManager){
        this.appWidgetManager = appWidgetManager;
        remoteViews =新RemoteViews(context.getPackageName(),R.layout.main);
        thisWidget =新的组件名(背景下,HelloWidget.class);
    }

    @覆盖
    公共无效的run(){
        remoteViews.setTextViewText(R.id.widget_textview,TIME =+ format.format(新日期()));
        appWidgetManager.updateAppWidget(thisWidget,remoteViews);
    }

    }
}
 

下面就是我要的是点击按钮后,被称为code:

 公共无效openWifiSettings(){
    最终意向意图=新的意图(Intent.ACTION_MAIN,NULL);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    最终的组件名CN =新的组件名(com.android.settings,com.android.settings.wifi.WifiSettings);
    intent.setComponent(CN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(意向);
}
 

解决方案

 进口android.app.PendingIntent;
...
公共静态最后弦乐ACTION_BUTTON1_CLICKED =com.example.myapp.BUTTON1_CLICKED;
 

的onupdate 以下内容添加到您的按钮:

 意向意图=新的意图(ACTION_BUTTON1_CLICKED);
PendingIntent pendingIntent = PendingIntent.getBroadcast(上下文,0,意图,0);
remoteViews.setOnClickPendingIntent(R.id.button_1,pendingIntent);
 

的onReceive 您AppWidget的

  @覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
 Log.d(TAG的onReceive()+ intent.getAction());
 super.onReceive(背景下,意图);
...
如果(ACTION_BUTTON1_CLICKED.equals(intent.getAction()))
{
 //你的code
}
 

也是你的意图添加到清单

 <意向滤光器>
   <作用机器人:名称=com.example.myapp.BUTTON1_CLICKED/>
   ....
 

Following this tutorial I quickly created a simple widget which displays the current time on the home screen:

I've modified the code so that the widget also comes with a button. My goal is to launch an activity, once the button get's pressed. Unfortunately, I do not really understand where to listen to the button click. Does this have to go into the broadcast receiver section of the manifest file?

<!--  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>    

Or does the code rather go into onUpdate? Here's the widget class code:

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);
    }

    } 
}

Here's the code that I want to be called after the button is clicked:

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";

in onUpdate add the following to your button:

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

and in onReceive of your AppWidget

@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
}

also add your intent to the manifest

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

这篇关于如何启动插件按钮后,活动pressed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆