【问题标题】:how to make the text of a widget launch an activity如何使小部件的文本启动活动
【发布时间】:2014-03-01 09:16:59
【问题描述】:

请问我需要帮助,我怎样才能让我的小部件上的文本启动一个活动到应用程序中文本(信息)所在的位置,或者它应该启动文本所在的活动。回答时,请在下面编辑我的代码。很乐意接受有效的答案。

public class WidgetUpdateService extends Service implements Handler.Callback{

String title;
String content;

 @Override
 public int onStartCommand(Intent intent, int flag, int startId) {
     handleCommand(intent);
     stopSelf();
    return START_STICKY;
 }

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void handleCommand(Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());

    int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    for (int widgetId : allWidgetIds) {
          // create some random data
            Log.i("WIDGET SERVICE STARTED", "Fetching");
          RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.cepf_appwidget_layout);
          remoteViews.setViewVisibility(R.id.widget_pb, View.VISIBLE);
          fetchFeed(remoteViews, widgetId, appWidgetManager);


          // Register an onClickListener
          Intent clickIntent = new Intent(this.getApplicationContext(), CepfWidgetProvider.class);

          clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
          clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

          PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
         //this is for the refresh button
          // remoteViews.setOnClickPendingIntent(R.id.sync_button, pendingIntent);
          //appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
}

private void fetchFeed(RemoteViews rm, int widgetId, AppWidgetManager mng){
    new Thread(new FetchTask(rm, widgetId, mng)).start();
}

class FetchTask implements Runnable{
    private RemoteViews rm;
    private int widgetId;
    private AppWidgetManager manager;
    public FetchTask(RemoteViews remoteView, int widgetId, AppWidgetManager manager){
        rm = remoteView;
        this.manager = manager;
        this.widgetId = widgetId;
    }

    @Override
    public void run() {
        String result=null;

        try{
            HttpClient httpclient=new DefaultHttpClient();
            HttpGet httppost=new HttpGet("");
            HttpResponse response=httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);

            JSONObject json_data = new JSONObject(result);
            JSONArray query = json_data.getJSONArray("query");
            final JSONObject post = query.getJSONObject(0);

            Log.i("WIDGET SERVICE CONTENT", post.getString("content"));
            Log.i("WIDGET SERVICE TITLE", post.getString("title"));

            title = post.getString("title");
            content = post.getString("content");

            //Message msg = Message.obtain();
            //Bundle bundle = new Bundle();
            //bundle.putString("title", title);
            //bundle.putString("content", content);
            //msg.setData(bundle);
            rm.setTextViewText(R.id.title_widget, title);
            rm.setTextViewText(R.id.desc_widget, content);
            rm.setViewVisibility(R.id.widget_pb, View.GONE);
            manager.updateAppWidget(widgetId, rm);

        }catch(IOException ioe){
            Log.i("WIDGET FETCH FEED", ioe.getMessage());
        }
        catch(JSONException je){
            Log.i("WIDGET FETCH FEED", je.getMessage());
        }

    }

}

private Handler mHandler = new Handler(this);

@Override
public IBinder onBind(Intent arg0) {

    return null;
}

@Override
public boolean handleMessage(Message msg) {

    return false;
}

}

CepfWidgetProvider:

public class CepfWidgetProvider extends AppWidgetProvider {
 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
            ComponentName thisWidget = new ComponentName(context,
             CepfWidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

            // Build the intent to call the service
            Intent intent = new Intent(context.getApplicationContext(),
                WidgetUpdateService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

            // Update the widgets via the service
            context.startService(intent);
    }
}

【问题讨论】:

    标签: android android-widget widget


    【解决方案1】:

    试试这个。

    您可以在 CepfWidgetProvider 类中创建此方法。

    private PendingIntent openActivity(Context context)
    {
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER); 
     // intent.putExtra(key, value); // just like you can do in startActivity(intent)
    
        // first param is app package name, second is package.class of the class you wish to open
        ComponentName cn = new ComponentName("com...", "com....ClassToOpenOnClick");
        intent.setComponent(cn);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return PendingIntent.getActivity(context, 0, intent, 0);
    }
    

    然后你需要在你的小部件中为你的文本视图添加一个点击监听器

    编辑:我注意到您应该首先在 onUpdate() 方法中的 CepfWidgetProvider.class 中尝试此操作。

    // Uncomment the line below if you do not have remoteViews already in your onUpdate.
    // RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.yourWidgetLayout);
    
    remoteViews.setOnClickPendingIntent(R.id.idOfYourWidgetTextView, openActivity(context));
    

    希望这对你有用! :)

    【讨论】:

    • @Losin 感谢您的回复,我会试试这个并立即回复您
    • @Losin what into into (context):- openActivity(context)-- context is this line is shown an error
    • 你太棒了,你真是个救世主
    猜你喜欢
    • 2010-12-28
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多