Notification应用
16lz
2021-01-26
阅读更多
package com.example.notificationtest;import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;public class MainActivity extends Activity {NotificationManager notificationManager;Spinner spinner;String[] arr = { "请选择", "face", "folder", "other", "text", "why" };ArrayAdapteradapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);spinner = (Spinner) findViewById(R.id.spinner);adapter = new ArrayAdapter (MainActivity.this,android.R.layout.simple_list_item_1, arr);adapter.setDropDownViewResource(R.layout.text);spinner.setAdapter(adapter);spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {if (position == 1) {setNotifi(R.drawable.face, arr[1]);}if (position == 2) {setNotifi(R.drawable.folder, arr[2]);}if (position == 3) {setNotifi(R.drawable.other, arr[3]);}if (position == 4) {setNotifi(R.drawable.txt, arr[4]);}if (position == 5) {setNotifi(R.drawable.why, arr[5]);}}@Overridepublic void onNothingSelected(AdapterView<?> parent) {// TODO Auto-generated method stub}});}private void setNotifi(int id, String text) {Intent intent = new Intent(this, Test.class);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);PendingIntent pend = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);Notification notification = new Notification();notification.icon = id;notification.tickerText = text;notification.defaults = Notification.DEFAULT_SOUND;notification.flags = Notification.FLAG_AUTO_CANCEL;notification.setLatestEventInfo(MainActivity.this, "MSN", text, pend);notificationManager.notify(0, notification);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
更多相关文章
- Gradle-Android打包文档
- android自定义弹出层
- 索尼阅读器 PRS-T1 被黑显露更多 Android(安卓)特性,可跑其它电子
- Android(安卓)阅读软件集锦 | 小众软件 > Android
- [置顶] 一路16有你,一起17前行。Keep不止,Android不息。
- 《Android(安卓)应用之路》 Android(安卓)开发技术选型(博客,新闻,
- Android消息机制源码阅读
- 阅读郭林《第一行代码》的笔记——第5章 全局大喇叭,详解广播机制
- 我的android阅读软件“微读”-做最简单的手机阅读软件