一、实验题目
Broadcast 使用和Widget 使用
二、要求
-
点击widget可以启动应用,并在widget随机推荐一个食品。
-
点击widget跳转到所推荐食品的详情界面。
-
点击收藏图标,widget相应更新。
-
点击widget跳转到收藏列表。
-
实现方式要求:启动时的widget更新通过静态广播实现,点击收藏图标时的widget更新通过动态广播实现。
三、实验结果
(1)实验截图
主页外观:
查看今日推荐:
点击今日推荐:
点击收藏:
点击弹出的收藏通知:
widget初始情况如下:
点击widget可以启动应用,并在widget随机推荐一个食品:
点击widget跳转到所推荐食品的详情界面:
点击收藏:
widget相应更新:
点击widget跳转到收藏列表:
(2)实验步骤以及关键代码
静态广播部分
-
StaticReceiver.java
定义静态广播接受类,注意channelID的设置和利用PendingIntent.FLAG_UPDATE_CURRENT获取及时更新。
package com.example.a13371.myapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import static android.content.Context.NOTIFICATION_SERVICE;
import static java.lang.System.exit;
public class StaticReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("here", context.toString());
if (intent.getAction().equals("STATIC")){
Log.i("where", "aaaaa");
Bundle bundle = intent.getExtras();
Collection c = (Collection)bundle.getSerializable("random");
int importance = NotificationManager.IMPORTANCE_HIGH;
//将channelID设置为here
NotificationChannel channel = new NotificationChannel("here", "this", importance);
channel.setDescription("www");
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
Intent mintent = new Intent(context, InfoActivity.class);
mintent.putExtra("Collection", c);
mintent.putExtra("flag", true);
mintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//对intent进行更新,这里一定不要设错了
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mintent, PendingIntent.FLAG_UPDATE_CURRENT);
//这里要写上对应的channelID
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "here")
.setSmallIcon(R.mipmap.empty_star)
.setContentTitle("今日推荐")
.setContentText(c.getName())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
Notification notify = mBuilder.build();
notificationManager.notify(1,notify);
}
}
}
-
AndroidManifest.xml
在此处对静态广播进行注册,并更改launchMode为"singleInstance"。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a13371.myapplication">
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.a13371.myapplication.InfoActivity"/>
<receiver android:name=".StaticReceiver">
<intent-filter>
<action android:name="STATIC" />
</intent-filter>
</receiver>
</application>
</manifest>
-
MainActivity.java
在此处产生随机推荐并发出静态广播。
//产生随机推荐
Random random = new Random();
int r = random.nextInt(clist.size()); //返回一个0到n-1的整数
Bundle bundle = new Bundle();
Intent intentBroadcast = new Intent("STATIC"); //定义Intent
bundle.putSerializable("random", clist.get(r));
ComponentName componentName = new ComponentName(getPackageName(),"com.example.a13371.myapplication.StaticReceiver");
intentBroadcast.setComponent(componentName);
intentBroadcast.putExtras(bundle);
sendBroadcast(intentBroadcast);
动态广播部分
-
DynamicReceiver.java
定义动态广播接受类,注意事项与静态一致。
package com.example.a13371.myapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class DynamicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("DDDD", context.toString());
if (intent.getAction().equals("DYNAMIC")) { //动作检测
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("there", "this", importance);
channel.setDescription("hhh");
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
Intent mIntent = new Intent(context, MainActivity.class);
mIntent.putExtra("like", "this");
//对intent进行更新
Bundle bundle = intent.getExtras();
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "there");
builder.setContentTitle("已收藏")
.setContentText(bundle.getString("name"))
.setSmallIcon(R.mipmap.empty_star)
.setWhen(System.currentTimeMillis())
.setTicker("您有一条新消息");
PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(mPendingIntent);
//绑定Notification,发送通知请求
Notification notify = builder.build();
notificationManager.notify(1, notify);
Log.i("DDDD", "finished");
}
}
}
-
InfoActivity.java
在点击收藏事件里对动态广播进行注册,activity结束后进行撤销。
Bundle bundle = new Bundle();
bundle.putString("name", sname);
Intent intent = new Intent("DYNAMIC");
intent.putExtras(bundle);
sendBroadcast(intent);
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(dynamicReceiver);
}
-
MainActivity.java
在此处接收动态广播,并调整可见状态。
//调整可见状态
@Override
protected void onNewIntent(Intent intent) {
Log.i("goout", "received");
super.onNewIntent(intent);
setIntent(intent);
Bundle extras = intent.getExtras();
if(extras != null) {
if(extras.getString("like").equals("this")) {
Log.i("why", extras.getString("like"));
Favorite.setVisibility(View.VISIBLE);
RView.setVisibility(View.INVISIBLE);
btn.setImageDrawable(getResources().getDrawable(R.mipmap.favorate));
click = !click;
}
}
}
EventBus部分
MessageEvent.java
package com.example.a13371.myapplication;
public class MessageEvent {
private String name;
private String circle;
public MessageEvent(String name, String circle) {
super();
this.name = name;
this.circle = circle;
}
public String getName() {
return name;
}
public String getCircle() {
return circle;
}
}
-
InfoActivity.java
在点击收藏事件里发送EventBus。
EventBus.getDefault().post(new MessageEvent(sname, scircle));
-
MainActivity.java
在此处注册接收和退出EventBus
//注册
EventBus.getDefault().register(this);
//收到消息
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
Map<String, Object> k = new LinkedHashMap<>();
k.put("cycle", event.getCircle());
k.put("name", event.getName());
this.favoritethings.add(k);
simpleAdapter1.notifyDataSetChanged();
}
//退出
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
widget部分
-
NewAppWidget.java
定义widget类,在其中完成初始化和更新方法。
package com.example.a13371.myapplication;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
RemoteViews updateView = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);//实例化RemoteView,其对应相应的Widget布局
Intent i = new Intent(context, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
updateView.setOnClickPendingIntent(R.id.wclick, pi); //设置点击事件
ComponentName me = new ComponentName(context, NewAppWidget.class);
appWidgetManager.updateAppWidget(me, updateView);
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
@Override
public void onReceive(Context context, Intent intent ){
super.onReceive(context, intent);
if(intent.getAction().equals("STATIC")){
Log.i("hihihi", "onReceive: ");
Bundle bundle = intent.getExtras();
Collection c = (Collection)bundle.getSerializable("random");
Intent mintent = new Intent(context, InfoActivity.class);
mintent.putExtra("Collection", c);
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mintent, PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setOnClickPendingIntent(R.id.wclick, pendingIntent);//给RemoteView上的Button设置按钮事件
updateViews.setTextViewText(R.id.appwidget_text, "今日推荐 "+c.getName());
ComponentName me = new ComponentName(context, NewAppWidget.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(me, updateViews);
}
}
}
-
AndroidManifest.xml
新增注册静态广播。
<receiver android:name=".NewAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="STATIC" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
-
new_app_widget.xml
定义widget的外观。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wclick"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<ImageView
android:id="@+id/appstar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:src="@mipmap/full_star" />
<TextView
android:id="@+id/appwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/appwidget_text"
android:text="@string/appwidget_text"
android:textColor="@color/white"
android:textSize="15sp"
android:textStyle="bold|italic"
android:layout_toRightOf="@+id/appstar"
android:layout_alignBaseline= "@+id/appstar"
android:layout_marginTop="17dp"
android:layout_marginLeft="3dp"
android:layout_alignParentTop="true"/>
</RelativeLayout>
-
new_app_widget_info.xml
定义widget的基本属性。
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/new_app_widget"
android:initialLayout="@layout/new_app_widget"
android:minWidth="300dp"
android:minHeight="50dp"
android:previewImage="@mipmap/full_star"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen|keyguard"></appwidget-provider>
-
MainActivity.java
在此处将随机推荐写入onRestart方法。
@Override
protected void onRestart() {
super.onRestart();
// 产生随机推荐
if (flag && !clist.isEmpty()) {
Random random = new Random();
int r = random.nextInt(clist.size()); //返回一个0到n-1的整数
Bundle bundle = new Bundle();
Intent intentBroadcast = new Intent("STATIC"); //定义Intent
bundle.putSerializable("random", clist.get(r));
ComponentName componentName = new ComponentName(getPackageName(),"com.example.a13371.myapplication.StaticReceiver");
intentBroadcast.setComponent(componentName);
intentBroadcast.putExtras(bundle);
sendBroadcast(intentBroadcast);
ComponentName wcomponentName = new ComponentName(getPackageName(),"com.example.a13371.myapplication.NewAppWidget");
intentBroadcast.setComponent(wcomponentName);
sendBroadcast(intentBroadcast);
flag = !flag;
}
}
-
DynamicReceiver.java
增添对widget接受动态消息的部分。
Intent mintent = new Intent(context, MainActivity.class);
mintent.putExtra("like", "this");
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mintent, PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setOnClickPendingIntent(R.id.wclick, pendingIntent);//给RemoteView上的Button设置按钮事件
updateViews.setTextViewText(R.id.appwidget_text, "已收藏 "+ bundle.getString("name"));
ComponentName me = new ComponentName(context, NewAppWidget.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(me, updateViews);
(3)实验遇到的困难以及解决思路
- 不知道如何获取bitmap。
参照这篇博客解决。
BitmapDrawable drawable = (BitmapDrawable)db; Bitmap bitmap = drawable.getBitmap(); Bitmap bt = BitmapFactory.decodeResource(context.getResources(), R.mipmap.empty_star);
- Android编译时提示“App is not indexable by Google Search; consider adding at least one Activity with an ACTION-VIEW”
参照这篇博客解决。
android { compileSdkVersion 28 defaultConfig { applicationId "com.example.a13371.myapplication" minSdkVersion 26 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } lintOptions { disable 'GoogleAppIndexingWarning' } productFlavors { } }
- 提示"On SDK version 23 and up, your app data will be automatically backed up and restored on app install."。
参照这篇博客解决
configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> def requested = details.requested if (requested.group == 'com.android.support') { if (!requested.name.startsWith("multidex")) { details.useVersion '28.0.0' } } } }
- 不知道如何查看断点。
参照这篇博客解决
- 修改版本时com.android.support冲突的解决办法。
参照这篇博客解决
- 静态广播无法被接收。
参照这篇博客解决
ComponentName componentName = new ComponentName(getPackageName(),"com.example.a13371.myapplication.StaticReceiver"); intentBroadcast.setComponent(componentName);
- 不知道android studio 有的代码加了删除线是怎么回事。
参照这篇博客解决
——说明是decrepted代码,这种代码在以前的版本适用,现在有新的代码来替代旧的。但是IDE依旧能识别所以能继续运行
- Android发通知 PendingIntent 中Intent 内容没有更新。
参照这篇博客解决
PendingIntent contentIntentBegin = PendingIntent.getActivity( notificationContext, 0, inStart, PendingIntent.FLAG_UPDATE_CURRENT);
- Android的onNewIntent方法不及时更新。
参照这篇博客解决
- 对Android生命周期有些不清楚。
参照这篇博客解决
这张图真是很优秀了:
- Android的onRestart方法调用时机。
参照这篇博客解决
(1)按下home键之后,然后切换回来,会调用onRestart()。
(2)从本Activity跳转到另一个Activity之后,按back键返回原来Activity,会调用onRestart();
(3)从本Activity切换到其他的应用,然后再从其他应用切换回来,会调用onRestart();
四、实验思考及感想
在这次实验当中,我最大的收获是学会了使用log来灵活的调试,以确定程序目前的运行状态,和在不同状态时各种变量的值,这让我能更快地找出问题所在,也更能深刻地理解Android中各个activity生命周期的演变。
附上一张调试时的妖艳截图: