我现在学到若水的service 第三讲,使用的是IntentService 为啥我用Log.i 就能呈现效果,使用Tosat 就 自动退出。。。。
现在我附送代码:
public class ExampleService extends IntentService
{
protected static final String TAG = "IntentActivity";
public ExampleService()
{
super("ExampleService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent)
{
new MyTherd().start();
}
private class MyTherd extends Thread
{
@Override
public void run()
{
// TODO Auto-generated method stub
try
{
String string="ExampleService线程ID:"+Thread.currentThread().getId()+"";
// Log.i(TAG,string);
// Log.i(TAG, "文件下载....");
Toast.makeText(ExampleService.this, string, Toast.LENGTH_LONG).show();
Thread.sleep(2000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
调用这个页面的代码为:
@Override
public void onClick(View v)
{
Button btn=(Button)v;
// TODO Auto-generated method stub
Intent intent =new Intent ();
switch (btn.getId())
{
case R.id.btnNormal:
intent.setClass(IntentServiceActivity.this, ExampleService.class);
startService(intent);
break;
case R.id.btnIntent:
intent.setClass(IntentServiceActivity.this, IntentService.class);
startService(intent);
break;
default:
break;
}
}
};