zoukankan      html  css  js  c++  java
  • 【XFeng安卓开发笔记】四大基本组件——跨应用启动service

    APP

    MainActivity.java
    package com.xfeng.startservicefromanotherapp;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //启动服务
            startService(new Intent(this,AppService.class));
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //停止服务
            stopService(new Intent(this,AppService.class));
        }
    }
    
    AppService.java
    package com.xfeng.startservicefromanotherapp;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    
    import java.sql.SQLOutput;
    
    public class AppService extends Service {
        public AppService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
    
            System.out.println("Service started");
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
    
            System.out.println("Service destory");
        }
    }
    

      

    AnotherApp  

    package com.xfeng.anotherapp;
    
    import android.content.ComponentName;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Intent serviceIntent;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            serviceIntent = new Intent();
            //显示intent
            serviceIntent.setComponent(new ComponentName("com.xfeng.startservicefromanotherapp", "com.xfeng.startservicefromanotherapp.AppService"));
    
            findViewById(R.id.btnStartAppService).setOnClickListener(this);
            findViewById(R.id.btnStopAppService).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btnStartAppService:
    
    //                Intent i = new Intent();
    //                //显示intent
    //                i.setComponent(new ComponentName("com.xfeng.startservicefromanotherapp","com.xfeng.startservicefromanotherapp.AppService"));
                    startService(serviceIntent);
                    break;
    
                case  R.id.btnStopAppService:
                    stopService(serviceIntent);
                    break;
    
    
            }
        }
    }
    

      

  • 相关阅读:
    Maven仓库是什么
    什么是Maven
    Shiro 的优点
    shiro有哪些组件
    Python偶斐波那契数
    Python求1000以内所有3或5的倍数的和。
    python"TypeError: 'NoneType' object is not iterable"错误解析
    python中列表常用的几个操作函数
    反射类的一些基本用法
    循环随机数短时间内大多都是重复的问题
  • 原文地址:https://www.cnblogs.com/xiaofeng6636/p/4986177.html
Copyright © 2011-2022 走看看