请我需要帮助,如何让我的小部件上的文本启动活动到文本(信息)在应用程序中的位置,或者它应该启动文本所在的活动。在回答时,请在下面编辑我的代码。我很乐意接受有效的答案。
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);
}
}
答案 0 :(得分: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));
希望这对你有用! :)