Android开发之自定义Notification(源代码分享)

    Android的自定义notification选项我觉得限制还是挺多的,如图所示,官方API告诉我们它依然必须得设置icon,tittle,text三个选项,除此之外,还一定要设置pengdingintent,不少网友还反映 builder.setContent(remoteViews)后面一定要紧跟着builder.setContentIntent(pendingIntent),要不然会报错误:android.app.RemoteServiceException: Bad notification posted from package

   MainActivity代码

package com.example.f04_notification;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {
	private NotificationManager manager;
	private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)this.findViewById(R.id.button1);
        //获取系统服务
        manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
				RemoteViews remoteViews=new RemoteViews(getPackageName(), R.layout.notifiaction);
				//因为android api的限制,自定义通知仍需要set small icon,setContentTittle,setContentText
				builder.setSmallIcon(R.drawable.ic_launcher);
				builder.setContentText("hello world!");
				builder.setContentTitle("Android");
				//以上内容仍必须设置一遍
				remoteViews.setTextViewText(R.id.textView1, "通知来了");
				remoteViews.setImageViewResource(R.id.imageView1, R.drawable.ic_launcher);
				Intent intent=new Intent(MainActivity.this,MainActivity.class);
				//pendingintent的flag分为四个FLAG_CANCEL_CURRENT,FLAG_NO_CREATE,FLAG_ONE_SHOT,FLAG_UPDATE_CURRENT	
				PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
				builder.setContent(remoteViews);
				builder.setContentIntent(pendingIntent);
				
				manager.notify((int)System.currentTimeMillis(), builder.build());
				
			}
		});
    }



    
}

布局xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:contentDescription="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="27dp"
        android:layout_marginTop="33dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_marginLeft="76dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="@string/hello_world" />

</RelativeLayout>

     感谢大家的阅读,希望跟大家一起互相学习,互相进步!

Android开发之自定义Notification(源代码分享),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。