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. 洛谷 U5737 纸条

    U5737 纸条 题目背景 明明和牛牛是一对要好的朋友,他们经常上课也想讲话,但是他们的班级是全校纪律最好的班级,所以他们只能通过传纸条的方法来沟通.但是他们并不能保证每次传纸条老师都无法看见,所以他 ...

  2. Oracle-定时任务

    PLSQL->新建->命令行窗口 --存储过程 create or replace procedure prd_remove_error_data AS BEGIN UPDATE rpt_ ...

  3. [asp.net]ashx中session存入,aspx为null的原因(使用flash uploader)

    I am using uploadify to upload files, they automatically post to the handler. I then modify the sess ...

  4. HDU 1042.N!【高精度乘法】【8月24】

    N! Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N ...

  5. [Phonegap+Sencha Touch] 移动开发19 某些安卓手机上弹出消息框 点击后不消失的解决的方法

    Ext.Msg.alert等弹出框在某些安卓手机上,点击确定后不消失. 原因是: 消息框点击确定后有一段css3 transform动画,动画完毕后才会隐藏(display:none). 有些奇葩手机 ...

  6. 【Hibernate步步为营】--单向关联一对一映射

    上篇文章对多对一的关联映射做了具体的分析,它在实现上能够有两种方式,而且这两种方式实现也非常easy,关键是标签<many-to-one>的使用,它分别指明了多端和一端的映射关系.这样的映 ...

  7. C# 监控Windows睡眠与恢复

    SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; private void SystemEvents_PowerModeC ...

  8. ios OpenCv的配置和人脸识别技术

    作为一个好奇心非常重的人,面对未知的世界都想去一探到底. 于是做了个人脸识别的demo. 眼下国内的关于opencv技术文章非常少.都是互相抄袭.关键是抄个一小部分还不全.时间又是非常久之前的了,和如 ...

  9. tp中使用事务

    是什么 事务是为了防止,多个操作,其中有失败,数据有部分被执行成功的时候使用的. 比如,银行,用户转账.张三钱扣了,结果李四钱还增加! 这个时候需要使用事务,确保张三钱扣了,李四的钱也增加,才真正的成 ...

  10. 使用VMware搭建3台一模一样的Linux虚拟机

    转自:https://www.linuxidc.com/Linux/2014-08/105909.htm 简介:VMware可以在个人本地一台笔记本机器上同时运行二个或更多Windows.DOS.LI ...