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. PHP学习总结(2)——PHP入门篇之PHP代码标识

    认识PHP代码标识 想在页面中编写PHP​代码非常容易,如下面代码: <?php echo "想学习php吗?来慕课网吧";?> 就像你可以编写JavaScript脚本 ...

  2. HDU 2196 Computer 树形DP经典题

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...

  3. HDU4763-Theme Section(KMP+二分)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. 在linux環境下安裝jprofiler_linux_8_0_2.sh

    1.安装jprofiler_linux_8_0_2.sh chmod+x jprofiler_linux_8_0_2.sh ./jprofiler_linux_8_0_2.sh –c 安装过程略..差 ...

  5. How to remove focus without setting focus to another control?

    How to remove focus without setting focus to another control? Ask Question up vote 67 down vote favo ...

  6. lightoj--1294--Largest Box(三分)

    Largest Box Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Sta ...

  7. kafka windows安装 命令行下使用测试

    1.zookeeper安装:   (https://zookeeper.apache.org/releases.html) ①进入zookeeper的相关设置所在的文件目录,例如本文的:D:\bigd ...

  8. python黏包现象

    #黏包:发送端发送数据,接收端不知道应如何去接收造成的一种数据混乱现象. #关于分包和黏包: #黏包:发送端发送两个字符串"hello"和"word",接收方却 ...

  9. SQL Server 忘记登录账号解决方法

    [1] 停止SQL Server 服务 和 SQL Server Agent 服务 [2] 以管理员身份打开命令行,单用户模式启动服务.(在单用户模式下启动 SQL Server 可使计算机本地 Ad ...

  10. FluentAPI关系映射配置

    都有哪几种关系? 1vs多,多vs多 1. 概念or关系映射相关方法: 1) 基本套路:this.Has***(o=>o.AAA).With***() 当前这个表和AAA属性的表关系是Has定义 ...