PS.不知不觉间发现自己已经做了很久很久的Android开发了,过去对知识块的梳理总是放在云笔记里面.主要的原因还是自己的笔记太杂乱,没有脉络.本着开源的精神,也趁着这段时间有空将之前云笔记里面的文章梳理下.同时将一些知识点整理出来和大家共同分享.
转载请注明出处:http://blog.csdn.net/unreliable_narrator?viewmode=contents
1.service就是就是一个没有界面的activity,同属于四大组件之一,使用的时候需要在清单文件中进行配置.service的主要作用就是在后台做一些比较耗时的操作.例如访问网络,后台播放,进行复杂计算等等.一般只要是满足于两个条件就可以使用service了,一是没有界面的交互,二是在后台长时间的运行.
startService(new Intent(MainActivity.this, MyService.class));
stopService(new Intent(MainActivity.this, MyService.class));
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("++", "MyService_onBind");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("++", "MyService_onUnbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.d("++", "MyService_onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("++", "MyService_onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("++", "MyService_onDestroy");
}
}
public class MainActivity extends AppCompatActivity {
private ServiceConnection serviceConnection;
private MyService.MyBinder mMyBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMyBinder = (MyService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
findViewById(R.id.btnOne).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//绑定开启服务
Intent intent = new Intent(MainActivity.this, MyService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
});
findViewById(R.id.btnTwo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用服务里面的方法
mMyBinder.doBinder();
}
});
}
}
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
MyBinder myBinder = new MyBinder();
Log.d("++", "MyService_onBind");
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("++", "MyService_onUnbind");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.d("++", "MyService_onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("++", "MyService_onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("++", "MyService_onDestroy");
}
/*binder对象
* */
public class MyBinder extends Binder {
public void doBinder() {
doSMinServices();
}
}
/*
* 服务里面的方法
* */
public void doSMinServices() {
Toast.makeText(MyService.this, "我是服务中的方法,我被调用了", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCreate() {
super.onCreate();
//创建通知对象
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentText("测试代码");
builder.setSmallIcon(R.drawable.biaoqin);
Notification build = builder.build();
//调用此方法将通知和服务绑定在一起
startForeground(1, build);
}
<!-- 为了消去加上android:priority="1000"后出现的警告信息,可以设置android:exported属性,指示该服务是否能够被其他应用程序组件调用或跟它交互 -->
<serviceandroid:name="com.example.helloandroid.weatherforecast.service.UpdateWidgetService"android:exported="false" >
<!-- 为防止Service被系统回收,可以通过提高优先级解决,1000是最高优先级,数字越小,优先级越低 -->
<intent-filter android:priority="1000"></intent-filter>
</service>