WEB开发网
开发学院手机开发Android 开发 实例示范如何使用 Android Services 阅读

实例示范如何使用 Android Services

 2010-03-03 15:48:00 来源:WEB开发网   
核心提示:Toast.makeText(this, “My Service Started”, Toast.LENGTH_LONG).show();Log.d(TAG, “onStart”);player.start();}}package com.example;import android.app.Service;impor

Toast.makeText(this, “My Service Started”, Toast.LENGTH_LONG).show();

Log.d(TAG, “onStart”);

player.start();

}

}

package com.example;

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.IBinder;

import android.util.Log;

import android.widget.Toast;

public class MyService extends Service {

private static final String TAG = “MyService”;

MediaPlayer player;

@Override

public IBinder onBind(Intent intent) {

return null;

}

@Override

public void onCreate() {

Toast.makeText(this, “My Service Created”, Toast.LENGTH_LONG).show();

Log.d(TAG, “onCreate”);

player = MediaPlayer.create(this, R.raw.braincandy);//运行例子是,需要替换音乐的名称

player.setLooping(false); // Set looping

}

@Override

public void onDestroy() {

Toast.makeText(this, “My Service Stopped”, Toast.LENGTH_LONG).show();

Log.d(TAG, “onDestroy”);

player.stop();

}

@Override

public void onStart(Intent intent, int startid) {

Toast.makeText(this, “My Service Started”, Toast.LENGTH_LONG).show();

Log.d(TAG, “onStart”);

player.start();

}

}

除此之外还要在Manifest里面声明服务:

Xml代码
<?xml version=”1.0″ encoding=”utf-8″?> 
<manifest xmlns:android=”http://schemas.android.com/apk/res/android“ 
  package=”com.example” android:versionCode=”1″ android:versionName=”1.0″> 
  <application android:icon=”@drawable/icon” android:label=”@string/app_name”> 
  

上一页  1 2 3 4 5  下一页

Tags:实例 示范 如何

编辑录入:coldstar [复制链接] [打 印]
赞助商链接