多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Notification用法

Notification用法

來源:程序員人生   發布時間:2015-01-24 09:15:49 閱讀次數:3179次

本文介紹了Notification的用法。

1、示例演示用法

1)NotificationActivity.java

/** * 演示了Notification的用法 * Notification的創建、顯示、刪除 * 通知欄點擊Notification打開Activity、Service、Broadcast * 直接new1個Notification或通過Notification.Builder來創建 * 自定義Notification的視圖并點擊交互 */ public class NotificationActivity extends Activity { private BroadcastReceiver mBroadcastReceiver; private NotificationManager mNotificationManager; private int NOTIFICATION_ID = 0x007; private int NOTIFICATION_ID2 = 0x008; private int NOTIFICATION_ID3 = 0x009; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(NewBroadcastReceiver.ACTION_NOTIFICATION); intentFilter.addAction(NewBroadcastReceiver.ACTION_NOTIFICATION2); mBroadcastReceiver = new NewBroadcastReceiver(); registerReceiver(mBroadcastReceiver, intentFilter); mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); findViewById(R.id.btn).setOnClickListener(mOnClickListener); findViewById(R.id.btn_direct).setOnClickListener(mOnClickListener); findViewById(R.id.btn_custom).setOnClickListener(mOnClickListener); //API level 11 Notification.Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher)//status bar .setTicker("猥瑣不")//status bar .setWhen(System.currentTimeMillis())//the time the event occurred .setDefaults(Notification.DEFAULT_SOUND) .setContentTitle("石鑫") .setContentText("小李飛刀") .setContentInfo("美好的回想") .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, NewActivity.class), 0))//startActivity // .setContentIntent(PendingIntent.getBroadcast(this, 0, // new Intent(NewBroadcastReceiver.ACTION_NOTIFICATION), 0))//sendBroadcastReceiver // .setContentIntent(PendingIntent.getService(this, 0, // new Intent(this, NewService.class), 0))//startService .setAutoCancel(true);//dismiss when touched Notification notification = builder.getNotification();//v16用build mNotificationManager.notify(NOTIFICATION_ID, notification); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mBroadcastReceiver); } private OnClickListener mOnClickListener = new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn: mNotificationManager.cancel(NOTIFICATION_ID);//通過ID刪除Notification mNotificationManager.cancel(NOTIFICATION_ID2); mNotificationManager.cancel(NOTIFICATION_ID3);break; case R.id.btn_direct: Notification notification = new Notification(R.drawable.ic_launcher, "monkey", System.currentTimeMillis()); //給Notification添加sound Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); notification.sound = ringURI; //給Notification添加Vibration long[] vibrate = new long[]{1000,1000,1000,1000,1000}; notification.vibrate = vibrate; notification.setLatestEventInfo(NotificationActivity.this, "Activity", "launch an activity", PendingIntent.getActivity(NotificationActivity.this, 0, new Intent(NotificationActivity.this, NewActivity.class), 0)); mNotificationManager.notify(NOTIFICATION_ID2, notification);break; case R.id.btn_custom: Notification notification2 = new Notification(R.drawable.ic_launcher, "monkey", System.currentTimeMillis()); RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notification); notification2.contentView = remoteView; notification2.contentIntent = PendingIntent.getActivity(NotificationActivity.this, 0, new Intent(NotificationActivity.this, NewActivity.class), 0); notification2.contentView.setTextViewText(R.id.txt, "騷包"); notification2.contentView.setProgressBar(R.id.progressbar, 1000, 400, false); //注冊及在廣播中處理點擊事件 notification2.contentView.setOnClickPendingIntent(R.id.img, PendingIntent.getBroadcast(NotificationActivity.this, 0, new Intent(NewBroadcastReceiver.ACTION_NOTIFICATION2), 0)); mNotificationManager.notify(NOTIFICATION_ID3, notification2); default: break; } } }; }
2)activity_main.xml
<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hit me" android:layout_gravity="center"/> <Button android:id="@+id/btn_direct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="direct" android:layout_gravity="center"/> <Button android:id="@+id/btn_custom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="custom" android:layout_gravity="center"/> </LinearLayout>
3)NewActivity.java
public class NewActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { Log.i(getClass().getSimpleName(), "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); } }
4)activity_new.xml
<?xml version="1.0" encoding="utf⑻"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="歡迎步入星光大道"/> </RelativeLayout>
5)NewBroadcastReceiver.java
public class NewBroadcastReceiver extends BroadcastReceiver { /**使用4次,定義、注冊、發送、接收*/ public static final String ACTION_NOTIFICATION = "action.NOTIFICATION"; public static final String ACTION_NOTIFICATION2 = "action.NOTIFICATION2"; @Override public void onReceive(Context context, Intent intent) { if(ACTION_NOTIFICATION.equals(intent.getAction())){ Log.i(getClass().getSimpleName(), "onReceive"); }else if(ACTION_NOTIFICATION2.equals(intent.getAction())){ Toast.makeText(context, "輕點好嗎", Toast.LENGTH_SHORT).show(); } } }
6)NewService.java
public class NewService extends Service { @Override public IBinder onBind(Intent intent) { //只有Service.onBind(Intent)覆寫 return null; } @Override public void onCreate() { Log.i(getClass().getSimpleName(), "onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(getClass().getSimpleName(), "onStartCommand"); return super.onStartCommand(intent, flags, startId); } }
7)notification.xml
<?xml version="1.0" encoding="utf⑻"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/> <ProgressBar android:id="@+id/progressbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/img" android:layout_alignParentBottom="true" style="@android:style/Widget.ProgressBar.Horizontal"/> </RelativeLayout>
8)AndroidManifest.xml
<?xml version="1.0" encoding="utf⑻"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.qinuli.notificationtest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.VIBRATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.qinuli.notificationtest.NotificationActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NewActivity"></activity> <service android:name=".NewService"/> </application> </manifest>

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 校园春色另类 | 亚洲欧美日韩国产精品 | 又粗又大又黄又爽的免费视频 | 欧美精品xxxxx| 精品国产日韩亚洲一区二区 | 老司机在线观看 | 老司机午夜精品视频 | 国产日韩视频在线观看 | 尤物免费在线视频 | 久久精品国产免费 | 在线精品国产第一页 | 国产一区二区三区播放 | 欧美精品色精品一区二区三区 | 日本亚洲精品成人 | 性欧美tubepornofree | 麻豆免费版在线观看 | 久久99久久99精品免观看麻豆 | freexx性| 一级做a爰片欧美一区 | 动漫日本在线免费观看 | 精品精品国产高清a毛片 | 最新国产中文字幕 | 免费观看视频网站 | 2020久久精品亚洲热综合 | 日韩一区国产二区欧美三区 | 欧美日韩成人在线视频 | 性做久久久久久蜜桃花 | 国产精品免费αv视频 | 波多野结衣xxxx性精品 | 一级做受毛片免费大片 | 久久婷婷人人澡人人爱91 | 永久免费毛片在线播放 | 呦视频在线一区二区三区 | 久久久欧美综合久久久久 | 亚洲综合日韩中文字幕v在线 | 久久久久久一级毛片免费无遮挡 | 国产亚洲第一页 | 麻豆va一区二区三区久久浪 | 欧美18videosex性欧美群 | 色xxxx| 国产精品免费久久久久影院小说 |