如何从Android中的小部件上的按钮运行活动?

时间:2012-03-30 15:59:09

标签: android button android-activity widget

我正在处理一个由按钮组成的切换小部件。按下时,我希望它能在不打开任何东西的情况下运行一个活动(就像平时一样在桌面上说)。有没有办法通过桌面小部件上的按钮直接运行活动?谢谢! 更新:现在我正在尝试从代码中切换静默模式而不运行新活动。 这是我当前的代码(当我因某些原因点击它们时按钮什么都不做)

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

    }

3 个答案:

答案 0 :(得分:7)

在主屏幕小工具上启动活动点击:

方法1:在onReceive

Intent intentClick =   the new Intent to (context, update, class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0,
intentClick, 0);
rv.setOnClickPendingIntent (R.id.layout, pendingIntent);

方法2:在onReceive

Intent intn = new Intent (context, update. Class);
intn.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity (intn);

但您必须为小部件点击广播注册

答案 1 :(得分:1)

如果你想做某事。在后台,您可以使用Service而不是Activity: http://developer.android.com/reference/android/app/Service.html

1.您可以在应用启动后立即进行starService,或者显示您的小部件。 http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent

2.单击widge时调用bindService http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent,android.content.ServiceConnection,int)

3.绑定后,您可以使用serviceConnection从绑定程序获取服务实例。然后,您可以执行服务中定义的代码。 请参阅此处的示例:http://developer.android.com/reference/android/app/Service.html

或者你可以

  1. 尝试启动服务(仅在服务实例上随时运行)

  2. 向要求服务的服务发送广播。对你而言。

  3. 由于活动旨在向用户显示某项内容,并且该服务旨在在后台执行。

答案 2 :(得分:0)

另一种方法是发送一个广播意图而不是开始一个活动!我想在按下小部件按钮时执行一个操作 - 不是专门用来启动将显示在屏幕上的活动。您可以使用此技术在小部件本身(其中小部件不一定包含活动)中执行您的代码。

Intent intt = new Intent(ctx, RWidg.class);
intt.setAction("myActionName");
PendingIntent bcInt = PendingIntent.getBroadcast(ctx, 0, intt, 0);

RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.widg_lay);
views.setOnClickPendingIntent(R.id.button, bcInt);

appWidgetManager.updateAppWidget(widgId, views);

不是使用 PendingIntent 来启动 Activity,而是使用 PendingIntent.getBroadcast() 创建一个带有“密钥”的 PendingIntent:ActivityManager.INTENT_SENDER_BROADCAST 这会导致 ActivityManager执行 sendBroadcast() 而不是 startActivity()

您需要在 onRecieve() 方法中添加“if”语句以拦截您使用的操作名称:

public void onReceive(Context ctx, Intent intt) {
  String action = intt.getAction();
  if (action.compareTo("myActionName")) {
    // Do your myActionName work here
  } else {
    super.onReceive(ctx, intt);
  }
} // end of onReceive