码迷,mamicode.com
首页 > 其他好文 > 详细

跨应用启动Service

时间:2016-06-16 23:16:30      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

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

 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

标签:

原文地址:http://www.cnblogs.com/linhongquan/p/5592288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!