service主要是用来提供服务功能的进程,和activity相比,偏向于无界面形式,
有几个地方需要注意:
1. service的onCreate,onStart是在主线程里执行的,也是通过ActivityThread的looper来进行调度的,可以再通过消息机制再来执行其他任务,一个耗时任务可能不会引起ANR,但是如果这个耗时任务后有一个UI任务,就会导致UI任务得不到及时响应,发生ANR,这里可以作为ANR知识点的一个关联点。
2. startService可以启动一个service进程,没有其他命令时,这个service进程阻塞在其进程的looper中。
3. binderService启动一个服务,会bind到进程中的一个binder对象,进而来实现相关Proxy功能调用,这个binder对象的方法是否是执行在binder线程中?这种情况,service是不是只是起到管理的作用?这些疑问,需要在进一步的学习中查看。
附一个service例子,使用一个服务对屏幕亮的次数进行记录显示
package com.example.myservice;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service{
private static final String TAG = "MyService";
public static int icount = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service created", Toast.LENGTH_LONG).show();
Log.i(TAG, "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stoped", Toast.LENGTH_LONG).show();
Log.i(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Start", Toast.LENGTH_LONG).show();
Log.i(TAG, "onStart");
//register an observer
IntentFilter filter_screen = new IntentFilter();
filter_screen.addAction(Intent.ACTION_SCREEN_ON);
filter_screen.addAction(Intent.ACTION_SCREEN_OFF);
getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_ON.equals(intent.getAction()))
{
icount++;
}
}
}, filter_screen);
}
}
package com.example.myservice;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity
implements OnClickListener {
private static final String TAG = "ServiceDemo";
public static int count = 0;
public static int sgTag = 0;
public TextView mTextView;
public void onClick(View src) {
count++;
mTextView.setText("click " + count);
if (sgTag == 0)
{
Log.i(TAG, "onClick: starting service");
startService(new Intent(this, MyService.class));
}
sgTag++;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);//activity_main
mTextView = (TextView)findViewById(R.id.textView1);
Button aButton = (Button)findViewById(R.id.button1);
aButton.setOnClickListener(this);
/*
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}*/
}
//use onResume to show
@Override
protected void onResume()
{
super.onResume();
mTextView.setText("screen on time is " + MyService.icount);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myservice.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:enabled="true" android:name=".MyService"/>
</application>
|