Android小部件并单击Listener
我正在尝试用启动时钟应用程序制作一个小部件 为什么这段代码在真实设备上不起作用 我做错了什么Android小部件并单击Listener,android,android-widget,appwidgetprovider,Android,Android Widget,Appwidgetprovider,我正在尝试用启动时钟应用程序制作一个小部件 为什么这段代码在真实设备上不起作用 我做错了什么 @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); PendingIntent pendingIntent; if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
PendingIntent pendingIntent;
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
{
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.clock_appwidget);
pendingIntent = PendingIntent.getActivity(context, 0,getAlarmPackage(context), 0);
views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
AppWidgetManager.getInstance(context).updateAppWidget(
intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
views);
}
}
public Intent getAlarmPackage(Context context)
{
PackageManager packageManager = context.getPackageManager();
Intent AlarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
String clockImpls[][] = {
{ "Standard Alarm", "com.android.alarmclock",
"com.android.alarmclock.AlarmClock" },
{ "HTC Alarm ClockDT", "com.htc.android.worldclock",
"com.htc.android.worldclock.WorldClockTabControl" },
{ "Standard Alarm ClockDT", "com.android.deskclock",
"com.android.deskclock.AlarmClock" },
{ "Froyo Nexus Alarm ClockDT",
"com.google.android.deskclock",
"com.android.deskclock.DeskClock" },
{ "Moto Blur Alarm ClockDT",
"com.motorola.blur.alarmclock",
"com.motorola.blur.alarmclock.AlarmClock" },
{ "Samsung Galaxy S", "com.sec.android.app.clockpackage",
"com.sec.android.app.clockpackage.ClockPackage" } };
boolean foundClockImpl = false;
for (int i = 0; i < clockImpls.length; i++)
{
String packageName = clockImpls[i][1];
String className = clockImpls[i][2];
try
{
ComponentName cn = new ComponentName(packageName, className);
packageManager.getActivityInfo(cn,PackageManager.GET_META_DATA);
AlarmClockIntent.setComponent(cn);
foundClockImpl = true;
} catch (NameNotFoundException nf)
{
Log.v("foundclock", nf.toString());
}
}
if (foundClockImpl)
{
return AlarmClockIntent;
}
else
{
return null;
}
}
@覆盖
公共void onReceive(上下文、意图)
{
String action=intent.getAction();
下垂的下垂的;
if(AppWidgetManager.ACTION\u APPWIDGET\u UPDATE.equals(ACTION))
{
RemoteView视图=新的RemoteView(context.getPackageName(),
R.布局、时钟(小部件);
PendingEvent=PendingEvent.getActivity(上下文,0,getAlarmPackage(上下文),0);
views.SetOnClickPendingEvent(R.id.imageView1,PendingEvent);
AppWidgetManager.getInstance(上下文).UpdateAppWidgetManager(
intent.getIntArrayExtra(AppWidgetManager.EXTRA\u APPWIDGET\u id),
意见);
}
}
公共意图getAlarmPackage(上下文)
{
PackageManager PackageManager=context.getPackageManager();
Intent AlarmClockIntent=新意图(Intent.ACTION\u MAIN).addCategory(Intent.CATEGORY\u启动器);
字符串clockImpls[][]={
{“标准闹钟”,“com.android.alarmclock”,
“com.android.alarmclock.alarmclock”},
{“HTC Alarm ClockDT”,“com.HTC.android.worldclock”,
“com.htc.android.worldclock.WorldClockTabControl”},
{“标准闹钟时钟”,“com.android.deskclock”,
“com.android.deskclock.AlarmClock”},
{“Froyo Nexus Alarm ClockDT”,
“com.google.android.deskclock”,
“com.android.deskclock.deskclock”},
{“Moto Blur Alarm ClockDT”,
“com.motorola.blur.alarmclock”,
“com.motorola.blur.alarmclock.alarmclock”},
{“三星Galaxy S”,“com.sec.android.app.clockpackage”,
“com.sec.android.app.clockpackage.clockpackage”};
布尔foundClockImpl=false;
对于(int i=0;i
我在emulator上运行应用程序,然后单击小部件-这项工作。在安卓4.0.3、2.2上测试可能会发现,您的新设备具有新的时钟实现,但不在clockImpls列表中。例如,如果您有新的Xperia Z时钟,则可以将其添加到列表中:
{ "Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" }
在某处创建完整的列表是个好主意。您必须放置更多日志语句,并检查它被阻止的位置。此接收器类别的目的过滤器是什么?或者它是AppWidgetProvider类?