0

I am trying to experiment with services, and I found this example: Alarm Manager Example. I tried using the accepted answer, and the changes AndroidDev suggested, but I cant get it to work. Based on the fact that none of my Log messages appear, I think that the Service isn't called.

My onCreate method is this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Intent serviceIntent = new Intent(this, YourService.class);
    startService(serviceIntent);
    Vibrator v=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
    // Vibrate for 500 milliseconds
    v.vibrate(500);


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

the Service is this:

package habos.alarmtest;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;


public class YourService extends Service
{
    Alarm alarm = new Alarm();
    String tag="Babis service";
    public void onCreate()
    {
    super.onCreate();
    Log.i(tag, "oncreate");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.i(tag, "onstartcommand");
    alarm.setAlarm(this);
    return START_STICKY;
}

@Override
public void onStart(Intent intent, int startId)
{
    alarm.setAlarm(this);
}

@Override
public IBinder onBind(Intent intent)
{
    return null;
}
}

The onStart method is deprecated, but onStartCommand should work.

the manifest is this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="habos.alarmtest">
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <receiver android:name=".Alarm" android:exported="true">
        <intent-filter>
            <action android:name="habos.START_ALARM" >
            </action>
        </intent-filter>
    </receiver>
    <activity
        android:name=".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>

and the alarm is this

package habos.alarmtest;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;
import android.os.Vibrator;


public class Alarm extends BroadcastReceiver
{
String tag="Babis alarm";
@Override
public void onReceive(Context context, Intent intent)
{
    Log.i(tag, "onreceive");
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    // Put here YOUR code.
    Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
    Vibrator v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
    // Vibrate for 500 milliseconds
    v.vibrate(500);

    wl.release();
}

public void setAlarm(Context context)
{
    Log.i(tag,"setalarm");
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent("mypackage.START_ALARM");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 30,pi); // Millisec * Second * Minute
}

public void cancelAlarm(Context context)
{
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}
}

added 2 more Logs, before and after the startService,the logcat is here: https://www.dropbox.com/s/0ksdrfzyjb0qafe/logcat.txt?dl=0

(sorry I didn't paste it directly here, the code block didn't work for some reason)

Charor
  • 94
  • 1
  • 12

0 Answers0