我刚刚开始玩Android中的服务,我正在尝试创建一个隐藏的应用程序,在启动时将手机静音。我正在关注这个帖子:Android hidden application
它没有显示任何错误,但服务在启动时不会被调用。
这是我的Android / manifest
<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>
我的serviceCode:`
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!
感谢您的帮助。
答案 0 :(得分:-1)
检查AndroidManifest中的包名称。
您使用android注册接收器:name =&#34; org.example.timer.serviceCode&#34;但在java类包中是package&#34; com.example.timer&#34;
UPD:
首先,您必须创建活动并至少启动一次。 http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
我只是自己测试 - 效果很好。