开发工具:Andorid Studio 1.3
运行环境:Android 4.4 KitKat
添加一个自定义Widget类,继承自AppWidgetProvider,重写onUpdate函数,通过RemoteView对象修改Widget中的TextView的值,AppWidgetManager被调用更新Widget
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Use RemoteView to update Widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
// Update AppWidget
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
添加一个自定义的广播接收器集成自BroadcastReceiver,重写onReceive函数,通过RemoteView对象修改Widget中的TextView的值为传入intent的消息,然后用AppWidgetManager发送RemoteView通知MyWidgetProvider更新内容
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent){
// Use RemoteViews to update Widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// Set editText
remoteViews.setTextViewText(R.id.widget_text, intent.getStringExtra("message"));
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context.getApplicationContext(),
MyWidgetProvider.class), remoteViews);
}
}
在MainActivity.java里面,新建一个MyBroadcastReceiver对象,给注册按钮添加响应动态注册和注销MyBroadcastReceiver,注册时设置的intentFilter内容需要和后面广播时一致
final Button regBroCast = (Button)this.findViewById(R.id.regBroadcast);
regBroCast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isRegister){
unregisterReceiver(myBroadcastReceiver);
regBroCast.setText(getResources().getString(R.string.regBroadcast));
isRegister = false;
} else {
registerReceiver(myBroadcastReceiver, new IntentFilter(getResources().getString(R.string.sysu)));
regBroCast.setText(getResources().getString(R.string.unRegBroadcast));
isRegister = true;
}
}
});
在MainActivity.java里面,给发送按钮添加响应,intent包中放入要修改的文字,给intent设置的文字需要和上面MyBroadcastReceiver中的intentFilter一致
Button send = (Button)this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getResources().getString(R.string.sysu));
intent.putExtra("message", mainEditText.getText().toString());
sendBroadcast(intent);
}
});
至此,关键内容已列出,程序运行正常
记得注册组件
<receiver
android:name=".MyWidgetProvider"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_provider" />
</receiver>
没注册时敲hello world并发送->界面没有内容出现->注册后发送->widget中出现hello world
传送门:下载