2012-07-13 38 views
10

Tôi đang tạo trình khởi chạy cho Android và tôi bị kẹt ở phần tiện ích. Tôi đã tìm kiếm khoảng một tiếng rưỡi trên internet cố gắng tìm ra cách tôi có thể lưu trữ các widget trong ứng dụng của mình, nhưng không may mắn.Lưu trữ các tiện ích trong trình khởi chạy android

tôi có đi qua một số phóng chứng khoán và mã launcher ADW nhưng cả hai chỉ có dặm mã và đây là lần đầu tiên tôi thực hiện một phóng.

Ai đó có thể vui lòng hướng dẫn tôi qua cách tôi có thể thêm tiện ích vào trình khởi chạy của mình? Hoặc ít nhất là đăng bất kỳ liên kết/hướng dẫn nào? Hãy giải thích vì đây là lần đầu tiên của tôi.

+1

Dưới đây là một lời giải thích tốt cùng với một dự án làm việc đầy đủ: http : //leonardofischer.com/hosting-android-widgets-my-appwidgethost-tutorial/ Nguồn: http://leonardofischer.com/wp-content/uploads/2012/01/WidgetHostExample.zip Chỉ cần áp dụng sửa lỗi này : http://selvaline.blogspot.it/2015/10/hosting-widgets-and-regurally-update.html – Pascal

Trả lời

14

Hãy thử điều này:

final int APPWIDGET_HOST_ID = 2048; 
final int REQUEST_PICK_APPWIDGET = 0; 
final int REQUEST_CREATE_APPWIDGET = 5; 

AppWidgetManager appWidgetManager; 
AppWidgetHost appWidgetHost; 

// Let user pick a widget from the list of intalled AppWidgets 
public void selectWidget() 
{ 
    int appWidgetId = this.appWidgetHost.allocateAppWidgetId(); 
    Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); 
    pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
    addEmptyData(pickIntent); 
    startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET); 
} 

// For some reason you have to add this empty data, else it won't work 
public void addEmptyData(Intent pickIntent) 
{ 
    ArrayList<AppWidgetProviderInfo> customInfo = 
     new ArrayList<AppWidgetProviderInfo>(); 
    pickIntent.putParcelableArrayListExtra(
     AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo); 
    ArrayList<Bundle> customExtras = new ArrayList<Bundle>(); 
    pickIntent.putParcelableArrayListExtra(
     AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras); 
}; 

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
           Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == REQUEST_PICK_APPWIDGET) { 
      configureWidget(data); 
     } 
     else if (requestCode == REQUEST_CREATE_APPWIDGET) { 
      createWidget(data); 
     } 
    } 
    else if (resultCode == RESULT_CANCELED && data != null) { 
     int appWidgetId = 
      data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 
     if (appWidgetId != -1) { 
      appWidgetHost.deleteAppWidgetId(appWidgetId); 
     } 
    } 
} 

// Show configuration activity of the widget picked by the user 
private void configureWidget(Intent data) { 
    Bundle extras = data.getExtras(); 
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 
    AppWidgetProviderInfo appWidgetInfo = 
      appWidgetManager.getAppWidgetInfo(appWidgetId); 
    if (appWidgetInfo.configure != null) { 
     Intent intent = 
      new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); 
     intent.setComponent(appWidgetInfo.configure); 
     intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
     startActivityForResult(intent, REQUEST_CREATE_APPWIDGET); 
    } else { 
     createWidget(data); 
    } 
} 

// Get an instance of the selected widget as a AppWidgetHostView 
public void createWidget(Intent data) { 
    Bundle extras = data.getExtras(); 
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); 
    AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId); 

    AppWidgetHostView hostView = appWidgetHost.createView(this, appWidgetId, appWidgetInfo); 
    hostView.setAppWidget(appWidgetId, appWidgetInfo); 
    // Add it on the layout you want 
    myLayout.addView(hostView); 
} 

// Call this when you want to remove one from your layout 
public void removeWidget(AppWidgetHostView hostView) { 
    appWidgetHost.deleteAppWidgetId(hostView.getAppWidgetId()); 

    // Remove from your layout 
    myLayout.removeView(hostView); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    appWidgetManager = AppWidgetManager.getInstance(this); 
    appWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID); 

    // Start listening to pending intents from the widgets 
    appWidgetHost.startListening(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    appWidgetHost.stopListening(); 
} 

Tất cả bạn phải làm là gọi phương thức selectWidget() bất cứ khi nào bạn muốn hiển thị một danh sách các AppWidgets cài đặt trên thiết bị để chọn từ.

Mã này là một phiên bản được sửa đổi của trình khởi chạy Android chứng khoán.

EDIT:

Để ràng buộc ID widget, tức là làm cho các widget cập nhật và đáp ứng tương tác người dùng, tham khảo các giải pháp ở đây: Widgets don't respond when re-added through code

+0

Đây là một plugin d mã chơi. Nếu bạn có bất kỳ nghi ngờ, bạn có thể yêu cầu. –

+0

Cảm ơn tôi sẽ thử ngay bây giờ – user1446632

+0

Không hoạt động. Không xảy ra bất cứ điều gì sau khi chọn một tiện ích ... – user1446632

Các vấn đề liên quan