跨应用启动Service

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了跨应用启动Service相关的知识,希望对你有一定的参考价值。

技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享

 1 package com.example.metrox.l16;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.IBinder;
 6 
 7 public class AppService extends Service {
 8 
 9 
10     public AppService() {
11     }
12 
13     @Override
14     public IBinder onBind(Intent intent) {
15         // TODO: Return the communication channel to the service.
16         throw new UnsupportedOperationException("Not yet implemented");
17     }
18 
19     @Override
20     public int onStartCommand(Intent intent, int flags, int startId) {
21         return super.onStartCommand(intent, flags, startId);
22     }
23 
24     @Override
25     public void onCreate() {
26         super.onCreate();
27         System.out.println("Service OnCreate");
28     }
29 
30     @Override
31     public void onDestroy() {
32         super.onDestroy();
33         System.out.println("Service OnDestroy");
34     }
35 }
 1 package com.example.metrox.l16;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 
 7 public class MainActivity extends AppCompatActivity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         startService(new Intent(this,AppService.class));
14     }
15 
16     @Override
17     protected void onDestroy() {
18         super.onDestroy();
19         stopService(new Intent(this,AppService.class));
20     }
21 }
 1 package com.example.testapp;
 2 
 3 import android.content.ComponentName;
 4 import android.content.Intent;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.TextView;
 9 
10 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
11 
12     private TextView textView;
13     private Intent intent;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_main);
18         intent = new Intent();
19         intent.setComponent(new ComponentName("com.example.metrox.l16","com.example.metrox.l16.AppService"));
20         textView = (TextView) findViewById(R.id.textView);
21         findViewById(R.id.btnStartAppService).setOnClickListener(this);
22         findViewById(R.id.btnStopAppService).setOnClickListener(this);
23     }
24 
25     @Override
26     public void onClick(View view) {
27         switch (view.getId()) {
28             case R.id.btnStartAppService:
29 
30                 startService(intent);
31                 break;
32             case R.id.btnStopAppService:
33                 stopService(intent);
34                 break;
35         }
36     }
37 }

 

以上是关于跨应用启动Service的主要内容,如果未能解决你的问题,请参考以下文章

跨应用启动Service并传递数据

Android跨应用启动

Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段

java [Intent] Intent片段以启动Activity,Service或发送广播。 #android_snippet #android

Messenger Service可以用于跨应用IPC吗?

学习笔记AIDL跨应用Service运用