2012-05-01 2 views
3

launch clock 응용 프로그램으로 위젯을 만들려고합니다.Android Widget을 클릭하고 Listener를 클릭하십시오.

왜이 코드는 실제 장치에서 작동하지 않습니까?

내가 뭘 잘못하고있어?

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

} 

에뮬레이터에서 응용 프로그램을 실행하고 위젯을 클릭하십시오. 안드로이드 4.0.3에서 테스트했습니다. 2.2

+0

더 많은 로그 문을 넣고 차단되는 위치를 확인해야합니다. 이 수신기 클래스의 인 텐트 필터는 무엇입니까? 아니면 AppWidgetProvider 클래스입니까? – San

답변

0

아마도 clockImpls 목록에없는 새 클럭 구현을 가진 새 장치가있을 것입니다. 새로운 엑스 페리아 Z 시계가있는 경우 예를 들어, 목록에 추가 할 수 있습니다

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

멋진 아이디어를 어딘가에 목록을 만들 수 있습니다.

관련 문제