IntentService
import android.app.IntentService;
public class MyIntentService extends IntentService
{
public MyIntentService()
{
//工作线程名
super("Working Thread");
}
/*模拟下载*/
protected void onHandleIntent(Intent intent)
{
for(int i=0;i<5;i++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
Log.i("info","正在下载...")
}
}
}
配置
<service android:name=".MyIntentService"></service>
启动
按钮
MainActivity.java
Intent intent = new Intent(MainActivity.this, MyIntentService.class);
startService(intent);