Android开发之自定义Notification(源代码分享)
2014-03-23 10:26
471 查看
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开发之自定义TabHost文字及背景(源代码分享)
- Android开发之自定义dialog的实现(源代码分享)
- Android开发之使用BroadcastReceiver实时监听电量(源代码分享)
- Android腾讯微薄客户端开发十五:终结篇(分享源代码)
- Android开发之自定义Spinner样式的效果实现(源代码实现)
- Android腾讯微薄客户端开发十五:终结篇(分享源代码)
- Android开发之Serializable 和 Parcelable的区别(源代码分享)
- Android开发之发送邮件功能的实现(源代码分享)
- Android开发之使用Handler封装下载图片工具类(源代码分享)
- Android开发之通过CursorAdapter读取数据(源代码分享)
- Android开发之Service与Activity数据交互(源代码分享)
- Android开发之使用BroadcastReceiver实现开机自动启动(源代码分享)
- Android开发之使用Preferences设计软件设置界面(源代码分享)
- Android Notification 开发经验分享
- Android开发之自定义Spinner样式的效果实现(源代码实现)
- Android开发之核心特性SearchView的开发使用(源代码分享)
- Android开发之android_apk 在线安装(源代码分享)
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- Android UI开发第二十五篇——分享一篇自定义的 Action Bar
- Android开发之手机拍照功能的实现(源代码分享)