Notification
2015-11-14 23:51
232 查看
MainAcivity.java
Second.java
AndroidMainfest.xml
activity_main.xml
second.xml
package com.hd.Notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Notification.Builder builder; private Button button; private NotificationManager manager; private Notification notification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); builder = new Notification.Builder(MainActivity.this); button = (Button) findViewById(R.id.button1); manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); builder.setTicker("有短信来了"); builder.setSmallIcon(R.drawable.ic_launcher); builder.setContentText("今晚有空吗"); builder.setContentTitle("短信"); builder.setAutoCancel(true);//点击通知之后,通知自动取消 Intent intent = new Intent(MainActivity.this, Second.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); builder.setContentIntent(pendingIntent); builder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE|Notification.DEFAULT_LIGHTS); notification= builder.build(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 manager.notify((int)System.currentTimeMillis(), notification); Log.i("MainAcitity", System.currentTimeMillis()+""); } }); } }
Second.java
package com.hd.Notification; import android.app.Activity; import android.os.Bundle; public class Second extends Activity{ public Second() { // TODO 自动生成的构造函数存根 } @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.second); } }
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hd.Notification" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.FLASHLIGHT"/> <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=".MainActivity" 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=".Second" android:label="@string/app_name" > </activity> </application> </manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hd.Notification.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="134dp" android:text="显示通知" /> </RelativeLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?> <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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
相关文章推荐
- EnumSet源码解析
- 顺序查找与二分查找
- 30分钟带你快速入门MySQL教程
- 【bzoj1052】[HAOI2007]覆盖问题
- how browser works
- IP组播技术介绍及实现例子
- 黑马程序员——File类及Properties类
- [kuangbin带你飞]专题一 简单搜索 总结
- 第一个Sprint冲刺第四天
- 个人阅读作业Week7
- (转载)深入PendingIntent
- Android 多线程断点下载project总结
- Java再学习——synchronized与volatile
- Step by Step Learn Python(1)
- 用gdb来调试除数为0而产生的core
- Python正则表达式指南
- 索引列的选择
- [阅读]个人阅读作业week7
- javax.net.ssl.SSLException: hostname in certificate didn't match:
- week7 read