通过点击小部件开始我的应用程序 [英] Start my application by clicking on widget
本文介绍了通过点击小部件开始我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图<一个href=\"http://stackoverflow.com/questions/9946469/how-do-i-run-an-activity-from-a-button-on-a-widget-in-android\">How我在Android的一个部件运行按钮活动?为了启动我的应用程序上点击图像按钮。当我取消注释code将其切换图像,但是当我尝试c将其无助于使用$ C $。我想,而不是拉开我的申请。
公共类MyWidgetIntentReceiver扩展广播接收器{ 私有静态诠释的clickCount = 0; @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(intent.getAction()。等于(pl.looksok.intent.action.CHANGE_PICTURE)){
updateWidgetPictureAndButtonListener(上下文);
}
} 私人无效updateWidgetPictureAndButtonListener(上下文的背景下){
Log.i(PROJECTCARUSO,updateWidgetPictureAndButtonListener);
RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget);
// remoteViews.setImageViewResource(R.id.widget_button,getImageToSet());
//
// //记得要经常刷新按钮单击听众!
// remoteViews.setOnClickPendingIntent(R.id.widget_button,MyWidgetProvider.buildButtonPendingIntent(上下文));
//
// MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),remoteViews); 意图intentClick =新意图(背景下,FragmentChange.class);
的PendingIntent的PendingIntent = PendingIntent.getActivity(上下文,0,
intentClick,0);
remoteViews.setOnClickPendingIntent(R.id.widget_button,的PendingIntent);
} 私人诠释getImageToSet(){
的clickCount ++;
返回的clickCount%2 == 0? R.drawable.app_icon:R.drawable.energy;
}
}
解决方案
下面就是我终于开始工作:
公共类WidgetProvider扩展AppWidgetProvider { 公共静态字符串SWITCH_WIDGET_UPDATE =MainActivity.Switch;
@覆盖
公共无效的onUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){ Log.i(PROJECTCARUSO,的onUpdate); RemoteViews remoteViews =新的RemoteViews(context.getPackageName(),R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.widget_button,buildButtonPendingIntent(上下文)); 意图intentClick =新意图(背景下,FragmentChange.class);
的PendingIntent的PendingIntent = PendingIntent.getActivity(上下文,0,intentClick,0);
remoteViews.setOnClickPendingIntent(R.id.widget_button,的PendingIntent); pushWidgetUpdate(背景下,remoteViews); }
公共静态的PendingIntent buildButtonPendingIntent(上下文的背景下){
意向意图=新的Intent();
intent.setAction(CHANGE_PICTURE);
返回PendingIntent.getBroadcast(背景下,0,意向,PendingIntent.FLAG_UPDATE_CURRENT);
}
公共静态无效pushWidgetUpdate(上下文的背景下,RemoteViews remoteViews){
组件名进myWidget =新的组件名(背景下,WidgetProvider.class);
AppWidgetManager经理= AppWidgetManager.getInstance(背景);
manager.updateAppWidget(进myWidget,remoteViews); } @覆盖
公共无效的onReceive(上下文的背景下,意图意图){
// TODO自动生成方法存根
super.onReceive(背景下,意图);
}
}
I'm trying How do I run an Activity from a button on a widget in Android? in order to launch my application on click on an image button. When i un-comment the code it switches the images but when i try to use this code it does nothing. I'd like to kick off my application instead.
public class MyWidgetIntentReceiver extends BroadcastReceiver {
private static int clickCount = 0;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_PICTURE")){
updateWidgetPictureAndButtonListener(context);
}
}
private void updateWidgetPictureAndButtonListener(Context context) {
Log.i("PROJECTCARUSO", "updateWidgetPictureAndButtonListener");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
// remoteViews.setImageViewResource(R.id.widget_button, getImageToSet());
//
// //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!!
// remoteViews.setOnClickPendingIntent(R.id.widget_button, MyWidgetProvider.buildButtonPendingIntent(context));
//
// MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
Intent intentClick = new Intent(context, FragmentChange.class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0,
intentClick, 0);
remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent);
}
private int getImageToSet() {
clickCount++;
return clickCount % 2 == 0 ? R.drawable.app_icon : R.drawable.energy;
}
}
解决方案
Here's what i finally got to work:
public class WidgetProvider extends AppWidgetProvider {
public static String SWITCH_WIDGET_UPDATE = "MainActivity.Switch";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.i("PROJECTCARUSO","onUpdate");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));
Intent intentClick = new Intent(context, FragmentChange.class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0, intentClick, 0);
remoteViews.setOnClickPendingIntent (R.id.widget_button, pendingIntent);
pushWidgetUpdate(context, remoteViews);
}
public static PendingIntent buildButtonPendingIntent(Context context) {
Intent intent = new Intent();
intent.setAction("CHANGE_PICTURE");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}
}
这篇关于通过点击小部件开始我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文