2012-05-01 78 views
3

我正在嘗試使用啓動時鐘應用程序製作小部件。Android小工具並單擊監聽器

爲什麼這段代碼在真實設備上不起作用?

我做錯了什麼?

@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; 
    } 

} 

我在模擬器上運行應用程序,並單擊小部件 - 這項工作。在android 4.0.3上測試,2.2

+0

你必須放更多的日誌語句,並檢查它被阻止的地方。這個接收器類的意圖過濾器是什麼?或者它是一個AppWidgetProvider類? – San

回答

0

也許,你有新的設備有新的時鐘實現,它不在clockImpls列表中。例如,如果您有新的Xperia Z ^時鐘您可以添加到列表:

{ "Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" } 

不錯的主意,地方創建完整列表。