下面是最简单的Android的消息推送的实现方法

package com.example.shownotic;

import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.MediaStore.Audio.Media;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends ActionBarActivity { private static final int STATUS_BAR_ID = 0;
Button btnShowNotic = null;
private Random random = new Random(20);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowNotic = (Button) findViewById(R.id.btnShowNotic);
btnShowNotic.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
public void run() {
while(true)
{
SystemClock.sleep(30000);
showNotic();
}
}
}).start();
showNotic();
} });
}
private void showNotic() {
NotificationManager notificationManger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Gigade";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon , tickerText , when );
notification.flags = Notification.FLAG_AUTO_CANCEL; CharSequence contentText = "show:"+random.nextInt(30);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.setData(Uri.parse("http://www.baidu.com"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent , 0);
notification.setLatestEventInfo(this, getTitle(), contentText , contentIntent ); notificationManger.notify(0, notification ); } }

Android开发学习笔记-关于Android的消息推送以及前后台切换的更多相关文章

  1. android开发学习笔记000

    使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...

  2. Android开发学习之路--Android Studio cmake编译ffmpeg

      最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...

  3. Android开发学习之路--Android系统架构初探

    环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...

  4. 【Android开发学习笔记】【第四课】基础控件的学习

    通过一个简单的例子来学习下面几种控件: 1.TextView:简单的文本显示控件 2.EditText:可以编辑的文本框 3.Button:按钮 4.Menu:这里指的是系统的Menu 5.Toast ...

  5. android开发学习笔记系列(1)-android起航

    前言 在学习安卓的过程中,我觉得非常有必要将自己所学的东西进行整理,因为每每当我知道我应该是如何去实现功能的时候,有许多细节问题我总是会遗漏,因此我也萌生了写一系列博客来描述自己学习的路线,让我的an ...

  6. 【转】Android开发学习笔记(一)——初识Android

    对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...

  7. Android开发学习笔记DDMS的使用

    打开DDMS DDMS 的全称是Dalvik Debug Monitor Service,是 Android 开发环境中的Dalvik虚拟机调试监控服务. DDMS里面包含了:Device(设备) F ...

  8. 【Android开发学习笔记】【高级】【随笔】插件化——Activity生命周期

    前言 如同第一章我们说的,宿主程序通过 dexclassloader 将插件的类加载进来,然后通过反射去调用它的方法,这样Activity就被当成了一个普通的类来执行了,因此系统不再接管它的生命周期, ...

  9. 【Android开发学习笔记】【第八课】五大布局-下

    概念 五大布局上一篇文章已经介绍了 LinearLayout RelativeLayout 这一篇我们介绍剩下的三种布局 FrameLayout 五种布局中最佳单的一种布局.在这个布局在整个界面被当成 ...

随机推荐

  1. InstallShield Build错误:Internal build error 6041

    点击左侧菜单: Media-Release-选择release版本(例如Release1)-Build标签-   keey unused directories 改为no(默认为yes)

  2. Repeater 嵌套,子级Repeater获取 父级Repeater 中的值

    第一种方法,子级Repeater中绑定父级的某个字段: <%# DataBinder.Eval((Container.NamingContainer.NamingContainer as Rep ...

  3. Listener 监听对象的创建和销毁

    HttpSessionListener.ServletContextListener.ServletRequestListener分别用于控制Session.context.request的创建和销毁 ...

  4. java socket 服务端 客户端

    Server package com.witwicky.socket.basicsocket; import java.io.IOException; import java.io.InputStre ...

  5. Item is not readable svn: 条目不可读

    问题:svn 查看资源历史记录失败 ,并提示"Item is not readable" 解决: 配置目录权限时如: [/]tangtx=rwyangcx=rwweishq=rw ...

  6. 模拟ajax实现网络爬虫——HtmlUnit

        最近在用Jsoup抓取某网站数据,可有些页面是ajax请求动态生成的,去群里问了一下,大神说模拟ajax请求即可.去网上搜索了一下,发现了这篇文章,拿过来先用着试试.    转帖如下: 网上关 ...

  7. C++ STL Maps

    Maps定义 --> 个人理解为python的字典 C++ Maps are sorted associative containers the contian unique key/value ...

  8. SQL Server2005配置同步复制

    因为实际需要,需要对两台SQL Server服务器配置同步复制.记录一下配置过程. 配置环境 两台服务器: 发布服务器,以下简称PS. 订阅服务器,以下简称SS. 两台服务器都是Windows Ser ...

  9. e858. 将键盘键和事件绑定

    This example creates a number of keystrokes and adds them to the input map of a component. When a ke ...

  10. Newtonsoft.Json 序列化小写首字母

    //json对象命名小驼峰式转换var json = JsonConvert.SerializeObject(newAccount, Formatting.Indented, new JsonSeri ...