您的位置:首页 > 移动开发 > Android开发

android 自定义NotifiCation

2015-11-17 19:18 405 查看
最近在做一个项目的时候,遇到一个这样的需求 客户需要在开机的时候有他们的广告,同时这个广告要一直都存在,先看一下效果。当然这里只计论技术,不讨论别的东西。





最终实现的效果是这样子,在这里说一下它里面所用到的技术及应该要注意哪些地方。

这里所用到的技术是 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() {
@Override
public 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: