I defined two background services for my app. But only one of the, the AttachService starts. In LogCat I can see the starting log messages of the attachService but nothing from the PenManagerService. I defined for each service a startup receiver
public class StartupCompletedReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, PenManagerService.class);
context.startService(service);
}
}
public class AttachmentStartupReceiver extends BroadcastReceiver {
private static final String TAG = AttachmentStartupReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
Intent attachSvc = new Intent(context, AttachService.class);
context.startService(attachSvc);
}
}
Here the definition of the two services in the manifest
<service
android:name="de.android.services.PenManagerService"
android:process=":PenManagerService" >
</service>
<service
android:name="de.android.services.AttachService"
android:icon="#drawable/loading_icon"
android:label="#string/attachServiceName"
android:process=":attachServiceBackground" />
<receiver
android:name="de.android.services.StartupCompletedReceiver"
android:process=":PenManagerService" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name="de.android.AttachmentStartupReceiver"
android:process=":attachServiceBackground" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Both services extends the IntentService and in their onCreate I have a log message, but only the one from the attachService shows up in the log.
Related
I want my app able to start an Activity whenever the app is terminated or running. but my code seems not work
<receiver
android:name=".BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
Log.e("BAM","ACTION_SCREEN_ON");
Intent i = new Intent(context, SecondMainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
should I call the BootReceiver in the OnCreate activity?
I have used BootComplete and allow permission and it still cant autostart,then I try to use wake lock but it cannot work. Also, I try to make it as a service but the service does not pop up in my phone.Is there anything I missed?
public class BootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
{
// This is the Intent to deliver to our service.
Intent serviceIntent = new Intent(context, AutoStartUp.class);
context.startService(serviceIntent);
}
}
public class AutoStartUp extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
// do something when the service is created
}
}
In my manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".SimpleWakefulReceiver">
<intent-filter>
<action android:name="com.example.SimpleWakefulReceiver"/>
</intent-filter>
</service>
<receiver
android:name=".MainActivity$BootComplete"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".AutoStartUp">
</service>
Do whatever you intend in onReceive -
public class BootupReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("BOOTUP", "received notification ......................");
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
{
Log.e("BOOTUP","RECEIVED BOOT NOTIFICATION ........");
Intent start_service = new Intent(context,MainService.class);
context.startService(start_service);
}
}
in the Manifest add-
<receiver
android:name=".AutoStart"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
add permission-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also you need to launch your application from activity atleast once and your block should be outside block in manifest
I have a COMPLETED ON BOOT service in which a music starts immediately should I receive an SMS.
Am I doing something wrong?
public class MyService extends Service {
final MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.son1);
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
AudioManager mAudioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
mPlayer.start();
}
}
};
}
<service android:name=".MyService"/>
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
I suggest you to use android:priority="999" in your AndroidManifest.xml as follows,
<receiver
android:name="com.application.reciever.SMSReceiver"
class="com.application.reciever.SMSReceiver" android:exported="true">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
This way your application get's priority on SMS Receive event.
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context mContext, Intent intent)
{
// Your code to play music
}
}
You probably miss the permission to receive SMS_RECEIVED broadcasts. Try adding to your manifest:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
I found lots of threads about this topic, however I'm not able to solve my problem. Here is the code:
The manifest file:
<service
android:name="com.xxx.player.MediaPlayerService.MediaPlayerService"
android:enabled="true" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
<receiver android:name="com.xxx.player.MediaPlayerService.MediaPlayerService$ServiceBroadcastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="#string/ACTION_PREPARE_AND_PLAY_NEW_FILE"/>
<action android:name="#string/ACTION_PREPARE_NEW_FILE"/>
<action android:name="#string/ACTION_START"/>
<action android:name="#string/ACTION_STOP"/>
<action android:name="#string/ACTION_PAUSE"/>
<action android:name="#string/ACTION_SEEK_TO"/>
<action android:name="#string/ACTION_RELEASE"/>
</intent-filter>
</receiver>
</service>
The broadcast class:
public class MediaPlayerService extends Service implements
MediaPlayer.OnErrorListener,
AudioManager.OnAudioFocusChangeListener,
Runnable,
SeekBar.OnSeekBarChangeListener {
...
public class ServiceBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(), "action: " + action, 30000000).show();
if (action.equals(Resources.getSystem().getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE))) {
prepareAndPlayNewFile(intent.getStringExtra("mediaData"));
}
}
}
}
The way I submit the intent:
private void prepareAndPlayNewFile(String mediaData) {
Intent myIntent = new Intent();
myIntent.setAction(context.getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE));
myIntent.putExtra("mediaData", mediaData);
context.sendBroadcast(myIntent);
}
Instead of approaching your playing of media this way, you should instead bind your Service to your Activity instead of using a broadcast receiver for message passing.
Also you shouldn't be using #string/VAR1 (which I'm not sure if intent filters work with that kind of string definition) for your intent actions it ALWAYS should be the constant string such as:
android.intent.action.BLAH
I have run into a snag, I can't get my alarms properly setup again after a boot.
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
if(fm.getAlarmBool()){
time = fm.getAlarmTimeLong();
reminder.startAlarm(time);
}
}
This is my method that I want to run after it boots up. I have added permissions in the android manifest but I can't get it to work. Whats wrong?
You should do it like
public class BootReceiver extends BroadcastReceiver {
Context context;
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, NotificationService.class));
}
}
in manifest
<receiver android:process=":remote" android:name="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</receiver>
so here in your service collect all the reminders and reschedule them.
for this you can store it in shared pref or database for persistent storage