我如何在Android的一个小部件运行按钮的活动? [英] How do I run an Activity from a button on a widget in Android?
问题描述
我工作的一个切换窗口小部件,它由一个按钮。当pressed,我想它,而无需打开任何东西(只是说在桌面上照常)中运行的活动。有没有一种方法可以直接运行在桌面小工具按钮preSS活动?谢谢! 更新:现在,我试图从内部code切换静音模式,不运行一个新的活动。 这是我目前的code(按钮做的,当我点击他们出于某种原因没有)
包toggleHD.widget;
进口android.app.Activity;
进口android.app.IntentService;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.media.AudioManager;
进口android.appwidget.AppWidgetManager;
进口android.appwidget.AppWidgetProvider;
进口android.content.ComponentName;
进口android.content.Context;
进口android.content.Intent;
进口android.util.Log;
进口android.widget.RemoteViews;
进口android.widget.Toast;
公共类ButtonWidget扩展AppWidgetProvider {
公共静态字符串ACTION_WIDGET_CLICK_RECEIVER =ActionReceiverWidget;
公共静态INT的appid [];
公共静态RemoteViews rview;
@覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
INT [] appWidgetIds){
updateWidgetState(上下文,);
}
@覆盖
公共无效的onReceive(上下文paramContext,意图paramIntent)
{
字符串str = paramIntent.getAction();
如果(paramIntent.getAction()。等于(ACTION_WIDGET_CLICK_RECEIVER)){
updateWidgetState(paramContext,STR);
}
其他
{
如果(android.appwidget.action.APPWIDGET_DELETED.equals(STR))
{
INT I = paramIntent.getExtras()调用getInt(appWidgetId,0);
如果(我== 0)
{
}
其他
{
INT [] arrayOfInt =新INT [1];
arrayOfInt [0] = I;
onDeleted(paramContext,arrayOfInt);
}
}
super.onReceive(paramContext,paramIntent);
}
}
静态无效updateWidgetState(上下文paramContext,字符串中的paramString)
{
RemoteViews localRemoteViews = buildUpdate(paramContext,中的paramString);
组件名localComponentName =新的组件名(paramContext,ButtonWidget.class);
AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName,localRemoteViews);
}
私有静态RemoteViews buildUpdate(上下文paramContext,字符串中的paramString)
{
// Toast.makeText(paramContext,buildUpdate()::+的paramString,Toast.LENGTH_SHORT).show();
rview =新RemoteViews(paramContext.getPackageName(),R.layout.main);
意图激活=新的意图(paramContext,ButtonWidget.class);
active.setAction(ACTION_WIDGET_CLICK_RECEIVER);
// PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext,0,活性,0);
// UPADTE这R.id.buttonus1与你的布局或图片ID在其上单击要开始活动
意图configIntent =新的意图(paramContext,TestReceiver.class);
configIntent.setAction(ACTION_WIDGET_CLICK_RECEIVER);
PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext,0,configIntent,0);
如果(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER))
{
//开放活动在这里..
//你的code进行更新,并且希望在按钮点击什么
//
rview.setOnClickPendingIntent(R.id.button_one,configPendingIntent);
}
返回rview;
}
@覆盖
公共无效onEnabled(上下文的背景下){
super.onEnabled(上下文);
// Toast.makeText(上下文,onEnabled(),Toast.LENGTH_SHORT).show();
}
//调用的应用程序窗口小部件的一个实例是从主机中删除每次
@覆盖
公共无效onDeleted(上下文的背景下,INT [] appWidgetId){
super.onDeleted(背景下,appWidgetId);
// Toast.makeText(上下文,onDeleted(),Toast.LENGTH_SHORT).show();
}
//调用时应用小工具的最后一个实例是从应用程序的Widget主机中删除。
@覆盖
公共无效onDisabled(上下文的背景下){
super.onDisabled(上下文);
// Toast.makeText(上下文,onDisabled(),Toast.LENGTH_SHORT).show();
}
}
在主屏幕上的Widget单击开始活动:
方法1:在的onReceive
意图intentClick =新的意向(背景下,更新类);
PendingIntent pendingIntent = PendingIntent.getActivity(上下文,0,
intentClick,0);
rv.setOnClickPendingIntent(R.id.layout,pendingIntent);
方法2:在的onReceive
意图INTN =新的意图(背景下,更新类);
intn.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(INTN);
但你必须注册一个广播的部件点击
I'm working on a toggle widget that consists of a button. When pressed, I'd like it to run an activity without opening up anything (just saying on the desktop as usual). Is there a way to directly run an activity from a button press on a desktop widget? Thanks! UPDATE: Now I'm trying to toggle silent mode from within the code without running a new activity. Here is my current code (the buttons do nothing when I click them for some reason)
package toggleHD.widget;
import android.app.Activity;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.media.AudioManager;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class ButtonWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget";
public static int appid[];
public static RemoteViews rview;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds){
updateWidgetState(context, "");
}
@Override
public void onReceive(Context paramContext, Intent paramIntent)
{
String str = paramIntent.getAction();
if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) {
updateWidgetState(paramContext, str);
}
else
{
if ("android.appwidget.action.APPWIDGET_DELETED".equals(str))
{
int i = paramIntent.getExtras().getInt("appWidgetId", 0);
if (i == 0)
{
}
else
{
int[] arrayOfInt = new int[1];
arrayOfInt[0] = i;
onDeleted(paramContext, arrayOfInt);
}
}
super.onReceive(paramContext, paramIntent);
}
}
static void updateWidgetState(Context paramContext, String paramString)
{
RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
ComponentName localComponentName = new ComponentName(paramContext, ButtonWidget.class);
AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
}
private static RemoteViews buildUpdate(Context paramContext, String paramString)
{
// Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
rview = new RemoteViews(paramContext.getPackageName(), R.layout.main);
Intent active = new Intent(paramContext, ButtonWidget.class);
active.setAction(ACTION_WIDGET_CLICK_RECEIVER);
// PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);
// upadte this R.id.buttonus1 with your layout or image id on which click you want to start Activity
Intent configIntent = new Intent(paramContext, TestReceiver.class);
configIntent.setAction(ACTION_WIDGET_CLICK_RECEIVER);
PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, configIntent, 0);
if(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER))
{
//open Activity here..
//your code for update and what you want on button click
//
rview.setOnClickPendingIntent(R.id.button_one, configPendingIntent);
}
return rview;
}
@Override
public void onEnabled(Context context){
super.onEnabled(context);
// Toast.makeText(context, "onEnabled() ", Toast.LENGTH_SHORT).show();
}
// Called each time an instance of the App Widget is removed from the host
@Override
public void onDeleted(Context context, int [] appWidgetId){
super.onDeleted(context, appWidgetId);
// Toast.makeText(context, "onDeleted() ", Toast.LENGTH_SHORT).show();
}
// Called when last instance of App Widget is deleted from the App Widget host.
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
// Toast.makeText(context, "onDisabled() ", Toast.LENGTH_SHORT).show();
}
}
start activity on Home Screen Widget click:
Method 1: in on onReceive
Intent intentClick = the new Intent to (context, update, class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0,
intentClick, 0);
rv.setOnClickPendingIntent (R.id.layout, pendingIntent);
Method 2: in onReceive
Intent intn = new Intent (context, update. Class);
intn.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity (intn);
but you must register a broadcast for widget click
这篇关于我如何在Android的一个小部件运行按钮的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!