1、android startservice无法启动服务

之前MainActivity.java中启动service源代码如下:

    private void startMyService()
{ //启动Service处理任务
Intent intent2 = new Intent(this, SmartCamService.class); Log.d("mainactivity》》","启动服务"+MainActivity.this.startService(intent2)); }

配置文件AndroidMainfest.xml代码如下:

   <application

        android:allowBackup="true"
android:icon="@mipmap/cap"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".main.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application>

这样运行老是启动不了服务,日志老是返回NULL

3、解决办法:

在配置文件中加入如下代码:

 <application

        android:allowBackup="true"
android:icon="@mipmap/cap"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".main.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service.SmartCamService"
android:label="SmartCamService"
android:enabled="true"
>
<intent-filter>
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /> </intent-filter>
</service> </application>

4、然后在再在MainActivity.java中加入如下代码:

    private void startMyService()
{ //启动Service处理任务
Intent intent2 = new Intent(this, SmartCamService.class);
intent2.setAction("android.intent.action.RESPOND_VIA_MESSAGE"); Log.d("mainactivity》》","启动服务"+MainActivity.this.startService(intent2)); }

5、现在启动程序,查看后台就会发现服务已经启动。

6、欢迎各位网友多多指教。

android startservice无法启动服务的更多相关文章

  1. Android使用bindService启动服务

    1.Service package com.example.ebobo; import java.util.Timer; import java.util.TimerTask; import andr ...

  2. 一个Demo学完Android中所有的服务(转)

    说明:这个例子实现了Android中常见的许多服务,下面是实现的截图 接下来,以源代码的方式分析这个例子   1.MainActivity--主界面 这个类主要是实现用户所看到的这个Activity, ...

  3. Android开发-API指南-服务

    Service 英文原文:http://developer.android.com/guide/components/services.html 采集(更新)日期:2014-12-23 原博客:htt ...

  4. Android初级教程启动定时器详解

    本案例知识是:后台执行定时任务. Alarm机制: 一.创建LongRunningService类 package com.example.servicebestpractice; import ja ...

  5. Android学习笔记_22_服务Service应用之—与Activity进行相互通信的本地服务

    一.启动服务的两种方法方法: 第一种:  startService()和stopService()启动关闭服务.适用于服务和Activity之间没有调用交互的情况.如果相互之间需要方法调用或者传递参数 ...

  6. Android四大组件之服务的两种启动方式详解

    Service简单概述 Service(服务):是一个没有用户界面.可以在后台长期运行且可以执行操作的应用组件.服务可由其他应用组件启动(如:Activity.另一个service).此外,组件可以绑 ...

  7. 【android学习1】:安装MySQL启动服务失败解决方法

    最近需要用到MySQL,从官网上下载了一个安装文件,但是安装时一直弹出如下提示信息: Configuration of MySQL Server 5.7 is taking longer than e ...

  8. Android开发学习—— Service 服务

    Service运行于后台的一个组件,用来运行适合运行在后台的代码,服务是没有前台界面,可以视为没有界面的activity. 服务可以被手动关闭,不会重启,但是如果被自动关闭,内存充足就会重启. sta ...

  9. Android 开机自启服务

    package com.example.lenovo.guangbo; import android.app.Service; import android.content.Intent; impor ...

随机推荐

  1. SpringBoot 国际化

    一.配置文件 二.application.properties 文件( 让国际化的文件被 SpringBoot 识别 ) spring.messages.basename=i18n.login 三.h ...

  2. BA-WG-泰豪发电机

    泰豪发电机的控制主板有2个端口,一个是RS232端口,一个是RS485端口,通常接网关需要将这个RS485的端口调整为modbus协议输出,再将modbus协议通过网关转换为bacnet / ip协议 ...

  3. iterator遍历list理解

    1.iterator的next()方法返回值,就是你指定的iiterator<>泛型.所以你再强制转换,就可以得到list里的item了,直接是item对象了. 2.list这东西,你ne ...

  4. poj1286 Necklace of Beads【裸polya】

    非常裸的polya,只是我看polya看了非常久 吉大ACM模板里面也有 #include <cstdio> #include <cmath> #include <ios ...

  5. java痛苦学习之路[四]---关于struts2-convention-plugin使用

    一.struts2-convention-plugin配置文件具体解释 <constant name="struts.convention.actionConfigBuilder&qu ...

  6. 【Java集合源代码剖析】LinkedList源代码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/35787253 您好.我正在參加CSDN博文大赛.假设您喜欢我的文章,希望您能帮我投一票,谢 ...

  7. Android获取系统时间的多种方法

    Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现. 现总结如下: 方法一: ? 1 2 3 4 5 ...

  8. python fuzzy c-means demo

    摘自:http://pythonhosted.org/scikit-fuzzy/auto_examples/plot_cmeans.html#example-plot-cmeans-py,加入了自己的 ...

  9. python中类的定义、实例化、封装以及私有变量/方法

    1.  定义类 python中定义一个类的格式如下: class MyClass(object): def __init__(self,data1,data2): self.__data1=data1 ...

  10. notepad++ 查找引用(Find Reference)(适用于c c++及各类脚本比如lua、python等)

    在程序开发过程中,程序员经常用到的一个功能就是查找引用(Find Reference),Visual Studio里面的对应功能是“查找所有引用”(Find All References). 我在使用 ...