notify service的简单实例

入口文件

InitActivity.java

package src.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class InitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent i = new Intent(this,NotifyService.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(i);//开启服务
}
}

NotifyService.java服务文件

package src.com;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class NotifyService extends Service {

private static final int HELO_ID = 1;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// Use with getSystemService(String) to retrieve a NotificationManager for informing the user of background events.
String ns = Context.NOTIFICATION_SERVICE;
//得到提醒管理对象
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
CharSequence tickerText = "记得缴钱";
//String tickerText = "记得缴钱";
long when = System.currentTimeMillis();//Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.

//Notification A class that represents how a persistent notification is to be presented to the user using the NotificationManager.
Notification notification = new Notification(R.drawable.ic_launcher, tickerText, when);

Context context = getApplicationContext();//Return the full application info for this context's package.
CharSequence contentTitle = "Libro来的提醒";
CharSequence contentText = "点击进入缴费清单";
Intent notificationIntent = new Intent(this, BillList.class);
//getActivity得到PendingIntent实例,即将进行某种操作
//getActivity(Context context, int requestCode, Intent intent, int flags)
//Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
//setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
//notify(int id, Notification notification)
//Post a notification to be shown in the status bar.
mNotificationManager.notify(HELO_ID, notification);////发出状态栏通知
}

}

BillList.java

package src.com;

import android.app.Activity;
import android.os.Bundle;

public class BillList extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bill);
}

}

bill.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bill1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bill2" />

</LinearLayout>

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="src.com"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".InitActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="NotifyService"></service>
<activity android:name="BillList"></activity>
</application>

</manifest>

原文地址:https://www.cnblogs.com/xingmeng/p/2425063.html