package com.example.servicedownlaod.service;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import com.example.servicedownlaod.MainActivity;
import com.example.servicedownlaod.R;
public class MyService extends Service {
public static final String TAG = "MyService";
public MyBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Notification mNotification = new Notification(R.drawable.ic_launcher, "有通知到来", System.currentTimeMillis());
Intent mNotificationIntent = new Intent(this ,MainActivity.class);
PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, mNotificationIntent, 0);
mNotification.setLatestEventInfo(this, "通知标题", "通知内容", mPendingIntent);
startForeground(1, mNotification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
// 开始执行后台任务
Log.d("TAG", "startDownload() executed");
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
public class MyBinder extends Binder{
public void startDownload() {
new Thread(new Runnable() {
@Override
public void run() {
// 执行具体的下载任务
Log.d("TAG", "startDownload() executed");
}
}).start();
}
}
}
activity
package com.example.servicedownlaod;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import com.example.servicedownlaod.service.MyService;
import com.example.servicedownlaod.service.MyService.MyBinder;
public class MainActivity extends Activity implements android.view.View.OnClickListener{
private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
private MyService.MyBinder mBinder;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mBinder = (MyBinder) service;
mBinder.startDownload();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService = (Button) findViewById(R.id.start_service);
stopService = (Button) findViewById(R.id.stop_service);
bindService = (Button) findViewById(R.id.bind_service);
unbindService = (Button) findViewById(R.id.unbind_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent);
break;
case R.id.bind_service:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, mServiceConnection, BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
unbindService(mServiceConnection);
break;
default:
break;
}
}
}
<service android:name="com.example.servicedownlaod.service.MyService"></service>
intentservice:
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService"); // 调用父类的有参构造函数
}
@Override
protected void onHandleIntent(Intent intent) {
// 打印当前线程的id
Log.d("MyIntentService", "Thread id is " + Thread.currentThread().
getId());
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyIntentService", "onDestroy executed");
}
}