Android开发之自定义Notification

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

\

MainActivity代码

01. package com.example.f04_notification;
02.  
03. import android.os.Bundle;
04. import android.app.Activity;
05. import android.app.NotificationManager;
06. import android.app.PendingIntent;
07. import android.content.Context;
08. import android.content.Intent;
09. import android.support.v4.app.NotificationCompat;
10. import android.view.View;
11. import android.widget.Button;
12. import android.widget.RemoteViews;
13.  
14. public class MainActivity extends Activity {
15. private NotificationManager manager;
16. private Button button;
17.  
18. @Override
19. protected void onCreate(Bundle savedInstanceState) {
20. super.onCreate(savedInstanceState);
21. setContentView(R.layout.activity_main);
22. button=(Button)this.findViewById(R.id.button1);
23. //获取系统服务
24. manager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
25. button.setOnClickListener(new View.OnClickListener() {
26.  
27. @Override
28. public void onClick(View v) {
29. // TODO Auto-generated method stub
30. NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
31. RemoteViews remoteViews=new RemoteViews(getPackageName(), R.layout.notifiaction);
32. //因为android api的限制,自定义通知仍需要set small icon,setContentTittle,setContentText
33. builder.setSmallIcon(R.drawable.ic_launcher);
34. builder.setContentText("hello world!");
35. builder.setContentTitle("<a href="http://www.it165.net/pro/ydad/" target="_blank" class="keylink">Android</a>");
36. //以上内容仍必须设置一遍
37. remoteViews.setTextViewText(R.id.textView1, "通知来了");
38. remoteViews.setImageViewResource(R.id.imageView1, R.drawable.ic_launcher);
39. Intent intent=new Intent(MainActivity.this,MainActivity.class);
40. //pendingintent的flag分为四个FLAG_CANCEL_CURRENT,FLAG_NO_CREATE,FLAG_ONE_SHOT,FLAG_UPDATE_CURRENT   
41. PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this1, intent, PendingIntent.FLAG_ONE_SHOT);
42. builder.setContent(remoteViews);
43. builder.setContentIntent(pendingIntent);
44.  
45. manager.notify((int)System.currentTimeMillis(), builder.build());
46.  
47. }
48. });
49. }
50.  
51.  
52.  
53.  
54. }

布局xml文件

01. <?xml version="1.0" encoding="utf-8"?>
02. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:layout_width="match_parent"
04. android:layout_height="match_parent" >
05.  
06. <ImageView
07. android:id="@+id/imageView1"
08. android:contentDescription="@string/hello_world"
09. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:layout_alignParentLeft="true"
12. android:layout_alignParentTop="true"
13. android:layout_marginLeft="27dp"
14. android:layout_marginTop="33dp"
15. android:src="@drawable/ic_launcher" />
16.  
17. <TextView
18. android:id="@+id/textView1"
19. android:layout_width="wrap_content"
20. android:layout_height="wrap_content"
21. android:layout_alignBottom="@+id/imageView1"
22. android:layout_marginLeft="76dp"
23. android:layout_toRightOf="@+id/imageView1"
24. android:text="@string/hello_world" />
25.  
26. </RelativeLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值