android 自定义NotifiCation
来源:互联网 发布:windows 2000 ghost 编辑:程序博客网 时间:2024/06/08 04:47
最近在做一个项目的时候,遇到一个这样的需求 客户需要在开机的时候有他们的广告,同时这个广告要一直都存在,先看一下效果。当然这里只计论技术,不讨论别的东西。
最终实现的效果是这样子,在这里说一下它里面所用到的技术及应该要注意哪些地方。
这里所用到的技术是 Notifaction ,这里所使用的布局是自定义View,当然一眼就只道这个布局很简单,只有一个 ImageView。
先上布局的代码。
<span style="font-size:14px;">custom_remote_view.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="wrap_content" ><span style="background-color: rgb(153, 255, 255);"> <ImageView android:id="@+id/imag" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/ims_contentview" android:paddingBottom="0dp" android:paddingTop="0dp" android:scaleX="1" android:scaleY="1" /> </span></RelativeLayout></span>
上面的代码是自定义 notifaction 的xml文档。
当然还有一个 就是在主布局里面 去点一下,不点也行,嘿嘿。
下面是 activity_main.xml的代码
<span style="font-size:14px;"><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=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <span style="background-color: rgb(153, 255, 255);"> <Button android:id="@+id/notification_test_button" android:layout_width="200dp" android:layout_height="60dp" android:text="发送notification" android:layout_centerInParent="true" /></span><span style="background-color: rgb(255, 102, 102);"></span></RelativeLayout></span>
下面是Java 代码 ,这里的代码 在 Activity 里面实现
<span style="font-size:14px;">package com.example.notificationtest;import android.os.Bundle;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.graphics.BitmapFactory;import android.support.v4.app.NotificationCompat;import android.support.v4.app.NotificationCompat.Builder;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RemoteViews;import android.widget.Toast;public class MainActivity extends Activity { Button sendNotiBtn; static NotificationManager notifyManager; static NotificationCompat.Builder mBuilder; public void initNotify(){ notifyManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); mBuilder = new NotificationCompat.Builder(this); mBuilder.setContentIntent(PendingIntent.getActivity(this, 1, new Intent(), 0)); mBuilder.setPriority(Notification.PRIORITY_DEFAULT); mBuilder.setOngoing(true); mBuilder.setSmallIcon(R.drawable.ic_launcher); mBuilder.setAutoCancel(false); } public void showNotifiCation(){ RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_remote_view);// remoteViews.setProgressBar(R.id.dxy_custom_notify_progress, 100, (int) percent, false); mBuilder.setContent(remoteViews); mBuilder.setContentIntent(PendingIntent.getActivity(this, 1, new Intent(), 0)); Notification notify = mBuilder.build(); notify.contentView = remoteViews; notifyManager.notify(103, notify); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initNotify(); sendNotiBtn = (Button)this.findViewById(R.id.notification_test_button); sendNotiBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {showNotifiCation();}}); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}</span>
代码确实比较简单,希望对需要的人有所帮助。
下面给出 Demo 的下载地址
http://download.csdn.net/detail/qijian0503/9277725
1 0
- Android自定义Notification
- Android自定义Notification
- Android自定义Notification布局
- Android自定义Notification
- Android 自定义的notification
- android自定义Notification
- Android-自定义Notification
- Android自定义Notification
- Android自定义Notification
- android notification 自定义声音
- Android-自定义Notification
- android自定义的notification
- android 自定义NotifiCation
- #Android 自定义之Notification
- android自定义notification UI
- Android Notification自定义样式
- Android自定义Notification通知
- Android Notification自定义布局实现
- Executors的工厂方法提供的5种不同的线程池
- 关于C++转换函数
- 新型网站开发之:从服务端取数据的最简方法
- 1099. Build A Binary Search Tree
- 回溯法-旅行售货员问题(C语言)
- android 自定义NotifiCation
- android 设计高性能的应用程序
- How Did Watson Answer —— Automatic Knowledge Acquisition
- ScrollView套ListView种种问题
- 修改 drawable 图片指定颜色
- POJ 2536 Gopher II (二分图最大匹配)
- word2vec模型
- AVL树
- POJ 1422 Air Raid 最小覆盖点