10 service 创建

来源:互联网 发布:淘宝网中老年秋装外套 编辑:程序博客网 时间:2024/05/19 18:10


清单文件添加权限

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


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.service.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.itheima.service.MyService" />          // 注意添加位置
    </application>


</manifest>


myservice 类

package com.itheima.service;


import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process;
import android.os.SystemClock;


public class MyService extends Service {

@SuppressWarnings("deprecation")
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();

Notification n = new Notification(R.drawable.ic_launcher, "MyService", System.currentTimeMillis());//  创建通知
Intent intent = new Intent();
intent.setClassName("com.itheima.service", "com.itheima.service.MainActivity");
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
n.setLatestEventInfo(this, "MyService", "MyService在后台长期运行", pi);

startForeground(Process.myPid(), n); //  前台服务,不会被杀死

//  客户端退出时   后天service   会后台退出,  能被杀死,但很难。  即便杀死也会复活 但不会执行start  执行create。  开3次,杀两次 第3次会复活?

}

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {  // 启动一个服务有两种方法
System.out.println("onStartCommand");     // 一种是onstart  一种是 onBind   多次调用同一个service 会多次调用start 但会只调一次creat 在同一个线程会阻塞

new Thread() {  //  只杀服务,还会打印,因为这是在一个新的线程。要停止,就要杀此进程。     
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
SystemClock.sleep(1000);
}
// stopSelf();// 无论调用了多少次start(), 都停止服务
stopSelf(startId);// 标记当前的这次start()停止, 等到所有start()都停止的时候, 停止服务
}
}.start();

return super.onStartCommand(intent, flags, startId);
}


@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return null;
}

@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}


}



界面

服务要从别的组件中启动  本咧是在按钮中启动

/****/

ackage com.itheima.service;


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


public class MainActivity extends Activity {
// private Intent intent=new Intent(this, MyService.class);   放在这里不可以,因为类还没创建,this 会空指针异常。????????????

private Intent intent

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, MyService.class);    // 显示意图 明显看见是哪个类    开启服务或activity 一般用intent
}


public void start(View v) {
startService(intent);// 启动服务, onStartCommand()
}

public void stop(View v) {
stopService(intent);// 停止服务, onDestroy()
}

}



0 0