我是靠谱客的博主 大气冬瓜,最近开发中收集的这篇文章主要介绍Android使用StartService、BindService以及IntentService简单实现Toast,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
一、实现效果
点击不同的按钮,用不同的方式开启/关闭Service,在屏幕底部出现Toast
二、步骤
1、构造MyService.java、MyBindService.java、MyIntentService.java
2、改变MainActivity.java的布局
3、在MainActivity中实现相应按钮的点击响应
4、在AndroidManifest.xml中声明几个Service类
三、代码
1、构造MyService.java、MyBindService.java、MyIntentService.java
- MyService.java
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Start Service", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Stop Service", Toast.LENGTH_LONG).show();
}
}
- MyBindService.java
public class MyBindService extends Service
{
public class MyBinder extends Binder
{
public MyBindService getService()
{
return MyBindService.this;
}
}
MyBinder myBinder = new MyBinder();
public void onCreate(){
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Toast.makeText(this, "Bind Service", Toast.LENGTH_LONG).show();
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "Unbind Service", Toast.LENGTH_LONG).show();
return false;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
- MyIntentService.java
public class MyIntentService extends IntentService {
private Handler handler;
public MyIntentService()
{
super("MyIntentService");
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
}
protected void onHandleIntent(Intent intent)
{
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Intent Service", Toast.LENGTH_SHORT).show();
}
});
}
}
2、改变MainActivity.java的布局
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="100dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<Button
android:id="@+id/start_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginVertical="10dp"
android:textAllCaps="false"
android:text="StartService"/>
<Button
android:id="@+id/stop_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginVertical="10dp"
android:textAllCaps="false"
android:text="StopService"/>
<Button
android:id="@+id/bind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginVertical="10dp"
android:textAllCaps="false"
android:text="BindService"/>
<Button
android:id="@+id/unbind_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginVertical="10dp"
android:textAllCaps="false"
android:text="UnbindService"/>
<Button
android:id="@+id/intent_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginVertical="10dp"
android:textAllCaps="false"
android:text="IntentService"/>
</LinearLayout>
</LinearLayout>
3、在MainActivity中实现相应按钮的点击响应
- MainActivity.java
public class MainActivity extends AppCompatActivity {
private boolean isBind = false;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
isBind=true;
MyBindService.MyBinder myBinder = (MyBindService.MyBinder)service;
MyBindService myBindService = myBinder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
isBind = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start_service = (Button) findViewById(R.id.start_service);
Button stop_service = (Button) findViewById(R.id.stop_service);
Button bind_service = (Button) findViewById(R.id.bind_service);
Button unbind_service = (Button) findViewById(R.id.unbind_service);
Button intent_service = (Button) findViewById(R.id.intent_service);
start_service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
});
stop_service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyService.class);
stopService(intent);
}
});
bind_service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyBindService.class);
bindService(intent,connection,BIND_AUTO_CREATE);
}
});
unbind_service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isBind)
{
unbindService(connection);
}
}
});
intent_service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
}
});
}
}
4、在AndroidManifest.xml中声明几个Service类
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practice6">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"></service>
<service android:name=".MyBindService"></service>
<service android:name=".MyIntentService"></service>
</application>
</manifest>
如果对你有帮助的话,欢迎点赞和收藏~
最后
以上就是大气冬瓜为你收集整理的Android使用StartService、BindService以及IntentService简单实现Toast的全部内容,希望文章能够帮你解决Android使用StartService、BindService以及IntentService简单实现Toast所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复