您的位置:首页 > 其它

消息提醒,初探Notification

2014-10-25 11:15 106 查看
1:MainActivity.java

package com.example.notificationtest;

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.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
private static  int NOTIFICATION_ID = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnSendNotification = (Button)findViewById(R.id.btnSendNotification);

btnSendNotification.setOnClickListener(btnOnClickListener());
}

OnClickListener btnOnClickListener(){
return new OnClickListener(){

@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.btnSendNotification:
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
String title = "消息标题";
intent.putExtra("title", title);

PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification n = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("消息提示").setContentTitle(title)
.setContentText("消息子标题")
.setContentInfo("消息内容")
.setWhen(System.currentTimeMillis())        //显示当前时间
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)    //使用默认的声音与震动
.setContentIntent(pi)
.setAutoCancel(true)
.build();

nm.notify(++NOTIFICATION_ID, n);
break;
}
}
};
}

}


2: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.example.notificationtest.MainActivity$PlaceholderFragment" >

<Button
android:id="@+id/btnSendNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/send_notification" />

</RelativeLayout>


3:NotificationActivity.java

package com.example.notificationtest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NotificationActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_notification);

TextView tvInfo = (TextView)findViewById(R.id.tvInfo);
tvInfo.setText(getIntent().getStringExtra("title"));

}
}


4:activity_notification.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"
android:gravity="center">

<TextView
android:id="@+id/tvInfo"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notificaiton Info"/>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: