Skip to content

Commit

Permalink
Lab3&4&5
Browse files Browse the repository at this point in the history
  • Loading branch information
HermanZYZ committed Oct 31, 2017
1 parent c2d70e7 commit 2a80693
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 9 deletions.
20 changes: 16 additions & 4 deletions Lab3/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:launchMode="singleInstance">
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -22,9 +24,19 @@
<intent-filter>
<action android:name="com.example.zyz.lab3.MyStaticFilter" />
</intent-filter>
<!--<intent-filter>-->
<!--<action android:name="com.example.zyz.lab3.MyDynamicFilter" />-->
<!--</intent-filter>-->
<!-- <intent-filter> -->
<!-- <action android:name="com.example.zyz.lab3.MyDynamicFilter" /> -->
<!-- </intent-filter> -->
</receiver>
<receiver android:name=".NewAppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.zyz.lab3.MyStaticFilter" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
</application>

Expand Down
1 change: 1 addition & 0 deletions Lab3/app/src/main/java/com/example/zyz/lab3/ItemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public void onClick(View view) {
Intent intentBoardcast = new Intent(DYNAMICACTION);
Bundle bundle = new Bundle();
bundle.putSerializable("item",items);
intentBoardcast.putExtra("toShoplist",false);
intentBoardcast.putExtras(bundle);
sendBroadcast(intentBoardcast);
EventBus.getDefault().post(new MessageEvent(items));
Expand Down
9 changes: 6 additions & 3 deletions Lab3/app/src/main/java/com/example/zyz/lab3/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private void Init()
Intent intentBoardcast = new Intent(STATICACTION);
Bundle bundle = new Bundle();
bundle.putSerializable("item",productList.get(tmp));
intentBoardcast.putExtra("toShoplist",false);
intentBoardcast.putExtras(bundle);
sendBroadcast(intentBoardcast);
}
Expand Down Expand Up @@ -145,7 +146,7 @@ public void convert(ViewHolder holder, Items items) {
@Override
public void onClick(int position) {
Intent newInten = new Intent().setClass(MainActivity.this, ItemInfo.class);
newInten.putExtra("position",position);
newInten.putExtra("toShoplist",false);
newInten.putExtra("item", productList.get(position));
startActivityForResult(newInten, 1);//启动intent
}
Expand Down Expand Up @@ -186,7 +187,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
public void onItemClick(int position){
// public void onItemClick(AdapterView<?> adapter,View view,int position,long l) {
Intent newInten = new Intent().setClass(MainActivity.this, ItemInfo.class);
newInten.putExtra("position", position);
newInten.putExtra("toShoplist",false);
newInten.putExtra("item", productList.get(position));
startActivityForResult(newInten, 2);//启动intent
}
Expand Down Expand Up @@ -230,7 +231,9 @@ public void onClick(DialogInterface dialogInterface, int i) { }
@Override
protected void onNewIntent(Intent intent)
{
setView(!HOME);
boolean check = intent.getBooleanExtra("toShoplist",false);
if(check)
setView(!HOME);
}
@Override
protected void onDestroy()
Expand Down
80 changes: 80 additions & 0 deletions Lab3/app/src/main/java/com/example/zyz/lab3/NewAppWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.example.zyz.lab3;

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.widget.RemoteViews;

/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider {
private static final String STATICACTION = "com.example.zyz.lab3.MyStaticFilter";
private static final String DYNAMICACTION = "com.example.zyz.lab3.MyDynamicFilter";
private Items items;

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);
Intent intent = new Intent(context,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.widget_image,pendingIntent);
ComponentName componentName = new ComponentName(context,NewAppWidget.class);
//views.setTextViewText(R.id.appwidget_text, widgetText);

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(componentName, views);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}

@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);
// AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
// Bundle bundle = intent.getExtras();
if(intent.getAction().equals(STATICACTION))
{
items = (Items) intent.getExtras().get("item");
assert items != null;
Intent local_intent = new Intent(context,ItemInfo.class);

Bundle bundle = new Bundle();
bundle.putSerializable("item",items);
local_intent.putExtras(bundle);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.new_app_widget);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,local_intent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setTextViewText(R.id.appwidget_text,items.getName()+"仅售¥ "+items.getPrice());
remoteViews.setImageViewResource(R.id.widget_image,items.getImg());
remoteViews.setOnClickPendingIntent(R.id.widget_image,pendingIntent);
ComponentName componentName = new ComponentName(context,NewAppWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName,remoteViews);
}
}

}

25 changes: 23 additions & 2 deletions Lab3/app/src/main/java/com/example/zyz/lab3/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RemoteViews;

/**
* Created by ZYZ on 2017/10/27.
Expand Down Expand Up @@ -43,6 +46,9 @@ public void onReceive(Context context, Intent intent)
.setAutoCancel(true); //用户单击面板即可取消通知
Intent mintent = new Intent(context,ItemInfo.class);
mintent.putExtras(intent.getExtras());
Bundle bundle = new Bundle();
bundle.putSerializable("item",item);
mintent.putExtras(bundle);
PendingIntent mPendingIntent = PendingIntent.getActivity(context,0,mintent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(mPendingIntent);
//绑定notification,发送通知请求
Expand All @@ -58,22 +64,37 @@ else if(intent.getAction().equals(DYNAMICACTION))
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//实例化通知状态栏构造器
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle("马上下单") //设置通知栏标题
builder.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle("马上下单") //设置通知栏标题
.setContentText(item.getName()+"已添加到购物车") //设置通知栏内容
.setTicker("您有一条新消息") //通知首次出现在通知栏,带上升动画效果
.setLargeIcon(bitmap) //设置通知大ICON
.setSmallIcon(R.mipmap.logo) //设置通知小ICON
.setColor(Color.parseColor("#FF0000")) //设置小图标颜色
.setAutoCancel(true); //用户单击面板即可取消通知
Intent mintent = new Intent(context,MainActivity.class);
mintent.putExtra("toShoplist",true);
Bundle bundle = new Bundle();
bundle.putSerializable("item",item);
bundle.putSerializable("items",item);
mintent.putExtras(bundle);
PendingIntent mPendingIntent = PendingIntent.getActivity(context,0,mintent,0);
builder.setContentIntent(mPendingIntent);
//绑定notification,发送通知请求
Notification notification = builder.build();
manager.notify(dynamication++, notification);

Intent local_intent = new Intent(context,MainActivity.class);
local_intent.putExtra("toShoplist",true);
Bundle bundle_wedget = new Bundle();
bundle.putSerializable("items",item);
local_intent.putExtras(bundle_wedget);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.new_app_widget);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,local_intent,PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setTextViewText(R.id.appwidget_text,item.getName()+"已加到购物车,马上下单");
remoteViews.setImageViewResource(R.id.widget_image,item.getImg());
remoteViews.setOnClickPendingIntent(R.id.widget_image,pendingIntent);
ComponentName componentName = new ComponentName(context,NewAppWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName,remoteViews);
}
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions Lab3/app/src/main/res/layout/new_app_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="horizontal"
android:padding="@dimen/widget_margin">

<TextView
android:id="@+id/appwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:background="#00000000"
android:contentDescription="@string/appwidget_text"
android:text="@string/appwidget_text"
android:layout_toRightOf="@+id/widget_image"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="bold|italic" />

<ImageView
android:id="@+id/widget_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:src="@drawable/shoplist"
app:srcCompat="@drawable/arla" />

</RelativeLayout>
10 changes: 10 additions & 0 deletions Lab3/app/src/main/res/values-v14/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--
Refer to App Widget Documentation for margin information
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
-->
<dimen name="widget_margin">0dp</dimen>

</resources>
10 changes: 10 additions & 0 deletions Lab3/app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--
Refer to App Widget Documentation for margin information
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
-->
<dimen name="widget_margin">8dp</dimen>

</resources>
2 changes: 2 additions & 0 deletions Lab3/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<string name="shopping_title">商品</string>
<string name="shopping_price">单价</string>
<string name="title_num">数量</string>
<string name="appwidget_text">当前没有任何信息</string>
<string name="add_widget">Add widget</string>

</resources>
10 changes: 10 additions & 0 deletions Lab3/app/src/main/res/xml/new_app_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen"></appwidget-provider>

0 comments on commit 2a80693

Please sign in to comment.