让自己的Android应用支持appwidget
2010-12-18 08:04:06 来源:WEB开发网import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
/**
* 添加appwidget
* @author chroya
*
*/
public class Main extends Activity {
private AppWidgetHost mAppWidgetHost;
private AppWidgetManager mAppWidgetManager;
private WidgetLayout layout;
private static final int REQUEST_PICK_APPWIDGET = 1;
private static final int REQUEST_CREATE_APPWIDGET = 2;
private static final int APPWIDGET_HOST_ID = 0x100;
private static final String EXTRA_CUSTOM_WIDGET = "custom_widget";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAppWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
mAppWidgetHost = new AppWidgetHost(getApplicationContext(), APPWIDGET_HOST_ID);
//开始监听widget的变化
mAppWidgetHost.startListening();
layout = new WidgetLayout(this);
layout.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
addWidget();
return false;
}
});
setContentView(layout);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_PICK_APPWIDGET:
addAppWidget(data);
break;
case REQUEST_CREATE_APPWIDGET:
completeAddAppWidget(data);
break;
}
} else if (requestCode == REQUEST_PICK_APPWIDGET &&
resultCode == RESULT_CANCELED && data != null) {
// Clean up the appWidgetId if we canceled
点击下载:php?id=1365" rel="nofollow">附件下载
更多精彩
赞助商链接