先看效果图
1.注册和发送广播活动 BroadActivity.java
public class BroadActivity extends AppCompatActivity {
private Button btnSendBroad;
MyBroadCast broadCast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad);
btnSendBroad = (Button)findViewById(R.id.btn_sendBroadCast);
broadCast = new MyBroadCast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.areyouok.xiaomi");
registerReceiver(broadCast, intentFilter);
btnSendBroad.setOnClickListener(view -> {
Fruit fruit = new Fruit("苹果", 8, "山东");
Gson gson = new Gson();
String strFruit = gson.toJson(fruit);
Intent intent = new Intent("com.areyouok.xiaomi");
intent.putExtra("fruit", strFruit);
sendBroadcast(intent);
});
}
class MyBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Gson gson = new Gson();
Fruit fruit = gson.fromJson(intent.getStringExtra("fruit"), new TypeToken<Fruit>(){}.getType());
if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel("myChannelId","channelname"
,NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(notificationChannel);
}
Bundle bundle = new Bundle();
bundle.putSerializable("apple", new Apple("富士", 45, false));
PendingIntent intent1 = PendingIntent.getActivity(context,0, IntentExtraClassActivity.createIntent(context, bundle), 0);
Notification notification = new NotificationCompat.Builder(context,"myChannelId")
.setContentTitle(fruit.fruitName)
.setContentText("水果的价格: "+fruit.fruitPrice+"元/斤 "+"水果产地: "+fruit.fruitAddress)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(intent1)
.build();
manager.notify(1,notification);
}
}
class Fruit {
private String fruitName;
private int fruitPrice;
private String fruitAddress;
public Fruit(String name, int price, String address){
this.fruitName = name;
this.fruitPrice = price;
this.fruitAddress = address;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadCast);
}
}
- 点击通知跳转到指定活动界面
public class IntentExtraClassActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_extra_class);
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("jump");
Apple apple = (Apple)bundle.getSerializable("apple");
Button button = (Button)findViewById(R.id.btn_get_apple);
Log.d("aaaaaa", apple.getType());
button.setOnClickListener(view -> {
Toast.makeText(IntentExtraClassActivity.this,apple.getType(), Toast.LENGTH_SHORT).show();
});
}
public static Intent createIntent(Context context, Bundle bundle){
Intent intent = new Intent(context, IntentExtraClassActivity.class);
intent.putExtra("jump", bundle);
return intent;
}
}
3.苹果数据类
public class Apple implements Serializable {
private String type;
private int price;
private boolean nothing;
public Apple(String t, int p, boolean n) {
this.type = t;
this.price = p;
this.nothing = n;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isNothing() {
return nothing;
}
public void setNothing(boolean nothing) {
this.nothing = nothing;
}
}
|