【问题标题】:Hidden app's service on startup not working启动时隐藏的应用程序服务不起作用
【发布时间】:2014-09-06 14:17:17
【问题描述】:

我刚开始在 Android 中使用服务,我正在尝试制作一个隐藏的应用程序,它可以在启动时将手机静音。我在关注这个帖子:Android hidden application

它没有显示任何错误,但服务不会在启动时被调用。

这是我的 Android/清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />



<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">

       <receiver android:name="org.example.timer.serviceCode" >
              <intent-filter >
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
              </intent-filter>
        </receiver>
         <service android:name="org.example.timer.service" />
</application>

还有我的服务代码:`

package com.example.timer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class serviceCode extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Intent i = new Intent("com.example.service");
        i.setClass(context, serviceCode.class);
        context.startService(i);
}

}

还有我的服务:

package com.example.timer;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;

public class service extends Service {
  private AudioManager myAudioManager;

  @Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override  
public void onCreate() {
        super.onCreate();
        myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        myAudioManager.setStreamMute(AudioManager.STREAM_ALARM, true);
        myAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
        myAudioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
        myAudioManager.setStreamMute(AudioManager.STREAM_RING, true);
        myAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
}
}

我有点困惑,因为我按照指南进行操作,但没有任何反应。当我打开手机时,声音是相同的值。我还尝试在那里吐司,看看服务是否启动但没有出现。

我的 LogCat:

[2014-09-06 16:13:46 - timer] ------------------------------
[2014-09-06 16:13:46 - timer] Android Launch!
[2014-09-06 16:13:46 - timer] adb is running normally.
[2014-09-06 16:13:46 - timer] No Launcher activity found!
[2014-09-06 16:13:46 - timer] The launch will only sync the application package on the device!
[2014-09-06 16:13:46 - timer] Performing sync
[2014-09-06 16:13:47 - timer] Uploading timer.apk onto device '0c295416'
[2014-09-06 16:13:47 - timer] Installing timer.apk...
[2014-09-06 16:13:49 - timer] Success!
[2014-09-06 16:13:49 - timer] \timer\bin\timer.apk installed on device
[2014-09-06 16:13:49 - timer] Done!

感谢您的帮助。

【问题讨论】:

    标签: android android-service


    【解决方案1】:

    检查 AndroidManifest 中的包名称。

    您使用 android:name="org.example.timer.serviceCode" 注册接收器,但在 java 类包中是包“com.example.timer”

    更新:

    首先,您必须创建活动并至少启动一次。 http://commonsware.com/blog/2011/07/05/boot-completed-regression.html

    我只是自己测试它 - 效果很好。

    【讨论】:

    • 其实我想做的是做一个应用程序,在学校时间自动静音我的手机。我不希望这个应用程序显示在应用程序列表中,因为我讨厌无用的图标。这就是为什么我决定制作一个隐藏的应用程序 - 没有活动。我想知道是否有办法让它成为可能。令人遗憾的是,它是这样的,因为隐藏的应用程序是唯一可以修改系统的东西,例如 Windows 的任务计划程序。如果你能推荐我这样做的方法,我会很高兴。另外我有一个root手机,如果有什么请分享。不过还是谢谢。
    • 有没有办法模拟用户交互以使应用程序在安装后从停止状态?或者有没有办法让活动在启动后自行删除,并在应用隐藏时让服务继续运行?
    • 我想我有一种方法适合你。只需使用命令行启动应用程序。看这里devmaze.wordpress.com/2011/12/05/activating-applications
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多