package com.example.twentytwocode;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* @author HD
* @date 2015-12-7
* @package_name com.example.twentytwocode
* @file_name MainActivity.java
*/
public class MainActivity extends Activity {
private Button btnStartService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStartService = (Button) findViewById(R.id.btnStartForeGroundService);
btnStartService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ForeGroundService.class);
startService(intent);
}
});
}
}
package com.example.twentytwocode;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
/**
* @author HD
* @date 2015-12-7
* @package_name com.example.twentytwocode
* @file_name ForeGroundService.java
*/
public class ForeGroundService extends Service {
@Override
public void onCreate() {
Log.i("hhhd", "oncreate");
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentText("这是一个通知");
builder.setContentTitle("通知");
builder.setTicker("ticker");
builder.setSmallIcon(R.drawable.ic_launcher);
Notification notification = builder.build();
startForeground(1, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
<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="com.example.twentytwocode.MainActivity" >
<Button
android:id="@+id/btnStartForeGroundService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="100dp"
android:layout_marginTop="132dp"
android:text="启动前台服务" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twentytwocode"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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="com.example.twentytwocode.ForeGroundService" >
</service>
</application>
</manifest>