我有一个多布局的小部件,这里有一些布局(每个作为主题),我需要为选定的主题(布局)分配所需的远程视图。与主题一样,我添加了 2 个接收器并为不同的布局(主题)创建了 xml 文件。现在我在列表中有一些小部件,没关系,但无法理解如何使用远程视图分配布局,因为可以选择任何布局。所以问题如下:如何使用接收器更正分配所需的布局(“A_Org_Widget”或“A_Org_Widget_right”),因为我需要创建功能,用户应该只从小部件列表中选择类型ot主题作为小部件名称。它需要如何做正确?
现在我的代码是:Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.widget.org"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7"/>
<application android:icon="@drawable/android3d_small" android:label="@string/app_name">
<receiver android:name="A_Org_Widget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.widget.org.ACTION_RECEIVER" />
</intent-filter>
<intent-filter>
<action android:name="com.widget.org.FORCE_WIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" />
</receiver>
<service android:name="A_Org_Widget$UpdateService" />
<receiver android:name="A_Org_Widget_right" android:label="A_Org_Widget_right">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_right" />
</receiver>
<receiver android:name="A_Org_Widget_small" android:label="A_Org_Widget_small">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_small" />
</receiver>
</application>
</manifest>
`
在代码中,接收器是跟随
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//context.startService(new Intent(context, UpdateService.class));
//this.widget_update(context, appWidgetManager, appWidgetIds);
if (true==A_Org_Widget.DBG) Log.d(LOG_TAG, " ===== onUpdate appWidgetIds.length="+appWidgetIds.length);
//for (int j = 0; j < appWidgetIds.length; j++)
{
if (0==AlreadyWasInited)
{
AlreadyWasInited=1;
if (true==A_Org_Widget.DBG) Log.d(LOG_TAG, " ===== onUpdate INIT " );
//Toast.makeText(context,"Updating onUpdate()", Toast.LENGTH_LONG).show();
//LappWidgetManager = appWidgetManager;
final AppWidgetManager LappWidgetManager=AppWidgetManager.getInstance(context);
//LappWidgetIds = appWidgetIds;
/*ComponentName*/ componentName=new ComponentName(context, A_Org_Widget.class);
LappWidgetIds=appWidgetManager.getAppWidgetIds(componentName);
////LappWidgetIds = Arrays.copyOf(appWidgetIds, appWidgetIds.length);
super.onUpdate(context, LappWidgetManager, LappWidgetIds);
if ("grey_bot"==A_Org_Widget.theme)
BasicView=BasicViewHorBott;
if ("grey_left"==A_Org_Widget.theme)
BasicView=BasicViewVerLeft;
String sptxt = context.getTheme().toString();
if (true==A_Org_Widget.DBG) Log.d(LOG_TAG, " ======= sptxt: " + sptxt);
Intent IntentCalls = new Intent(context, A_Org_Widget.class);
IntentCalls.setAction(ACTION_PHONECALLS);
// Get the layout for the App Widget and attach an on-click listener to the button
RView.setOnClickPendingIntent(R.id.ImageButton06, pIntentCalls);
RView.setViewVisibility(R.id.ImageArrowCalls, View.VISIBLE);
Collector.NumStringsToShow = 36;
//Collector.theme = "grey_left";
// INITIALISATION
if (0==Collector.ArrayInited)
{
Collector.ArrayInited++;
if (null!=Collector.ACalls)
{
for (int i=0; i < Collector.NumStringsToShow; i++)
{
Collector.ACalls[i] = new Struct_TP_String();
Collector.ASMS[i] = new Struct_TP_String();
Collector.AClndr[i] = new Struct_TP_String();
//if (Emulator.IF_NEW) RView.addView( R.id.STR00+i, Collector.ACalls[i].RViewSt );
//if (Emulator.IF_NEW) RView.setViewVisibility(R.id.STR00+i, View.VISIBLE);
}
}
Emulator.Prepare_Data_For_Emulate(context, RView);
} else {
if (true==A_Org_Widget.DBG) Log.d(LOG_TAG, " ======= ArrStrTmplStrings is already INITIALED!");
}
Collector.prev_key=0;
Collector.curr_key=1;
Emulator.ArabicStr=false;
Collector.Selector(context, RView, R.id.ImageButton06, LappWidgetIds, "");
Emulator.Prepare_Data_For_Emulate(context, RView);
Emulator.ShowFromEmulator(context, RView);
//appWidgetManager.updateAppWidget(appWidgetIds, RView);
context.sendBroadcast(new Intent(A_Org_Widget.FORCE_WIDGET_UPDATE));
//appWidgetManager.updateAppWidget(appWidgetIds/*[j]*/, RView);
}
// end of Init
else
// Ordinary tasks will be here in this block
{
// Show must go on...
if (true==A_Org_Widget.DBG) Log.d(LOG_TAG, " ======= onUpdate JOB! ");
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
final AppWidgetManager LappWidgetManager=AppWidgetManager.getInstance(context);
super.onReceive(context, intent);
AppWidgetProviderInfo appWidgetProviderInfo = new AppWidgetProviderInfo();
//String
int wwww=appWidgetProviderInfo.initialLayout;
Log.d(LOG_TAG, " ===== wwww=" +wwww);
if (1==AlreadyWasInited)
{
if (FORCE_WIDGET_UPDATE.equals(intent.getAction())) {
// TODO Обновить пользовательский интерфейс виджета.
Log.d(LOG_TAG, " ===== onReceive === FORCE_WIDGET_UPDATE" );
LappWidgetManager.updateAppWidget(A_Org_Widget.LappWidgetIds, RView);
}
if (true==A_Org_Widget.DBG)
Log.d(LOG_TAG, " ===== onReceive " );
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
if (true==A_Org_Widget.DBG) Log.d(LOG_TAG, " ======= onReceive: ACTION_APPWIDGET_DELETED");
final int appWidgetId
= intent.getExtras().getInt( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID );
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
this.onDeleted(context, new int[] { appWidgetId } );
}
}
else
{
int id_button=0;
//Collector.prev_key=Collector.curr_key;
if (intent.getAction().equals(ACTION_PHONECALLS) ) {
id_button = R.id.ImageButton06;
Collector.curr_key=1;
Emulator.ArabicStr=false;
}
.....
if (0!=id_button){
if (Collector.curr_key != Collector.prev_key){
Emulator.now_date=Calendar.getInstance();
Emulator.date1=Emulator.now_date.getTimeInMillis();
Log.d(LOG_TAG, " ======= KEY PRESSED, CALC TIME...");
Collector.Selector(context, RView, id_button, LappWidgetIds, "");
Emulator.Prepare_Data_For_Emulate(context, RView);
Emulator.ShowFromEmulator(context, RView);
//LappWidgetManager.updateAppWidget(A_Org_Widget.LappWidgetIds, RView);
context.sendBroadcast(new Intent(A_Org_Widget.FORCE_WIDGET_UPDATE));
}
}
}
}
}
`