1

I have a working instance of an Alarm - however when I attempt to create a 2nd Alarm - neither the 1st nor the 2nd intent never repeatedly execute and I'm really not sure why this is happening.

I have added a toast to each activity/service for testing purposes so I can tell if they are executing successfully (each one runs every 10 seconds).

When I click "OK" to start the two services - I see the toast for my first service - but the second one never initially appears - and nothing happens after that.

Any suggestions / input are greatly appreciated!

AlarmManager Alarm Service:

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);
        Intent intent = new Intent(Rules.this, LMW.class);
        PendingIntent pintent = PendingIntent.getService(Rules.this, 0, intent,
                0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                100000000 * 1000, pintent);

AlarmManager Activity:

      //  Calendar cal = Calendar.getInstance();
      //  cal.add(Calendar.SECOND, 10);
        Intent intent2 = new Intent(Rules.this, KillTimer.class);
        PendingIntent pintent2 = PendingIntent.getActivity(Rules.this, 0, intent2,
                0);
        AlarmManager alarm2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                30 * 1000, pintent2); // here    

    // click listener for the button to start service
    Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

FULL SOURCE:

public class Rules extends Activity {
    private String password;
    private  PendingIntent mPendingIntent;

TextView textSsid, textSpeed, textRssi;
private static final int NOTIFY_ME_ID=1337;
private int count=0;
private NotificationManager notifyMgr=null;
    public Handler mHandler = new Handler();
    public long mStartRX = 0;
    public long mStartTX = 0;
    public long txBytes;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rules);
    Parse.initialize(this, "7gjqmUcoqu1IZPJSSxXLdE4L8efAugCXA7snLSH6", "5NckF83MUBumQ8L8zL7Akc4p07beMRnmvgCfhZdH");

    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();

    // If you would like all objects to be private by default, remove this line.
    defaultACL.setPublicReadAccess(true);

    ParseACL.setDefaultACL(defaultACL, true);

textSsid = (TextView) findViewById(R.id.Ssid);
textSpeed = (TextView) findViewById(R.id.Speed);
textRssi = (TextView) findViewById(R.id.Rssi);
Long.toString(mStartTX);
Long.toString(mStartRX);
Long.toString(txBytes);
ParseObject testObject = new ParseObject("TestObject");
testObject.put("DataO", String.valueOf(mStartTX));
testObject.put("DataI", String.valueOf(mStartRX));

testObject.saveInBackground();

ParseAnalytics.trackAppOpened(getIntent());



mStartRX = TrafficStats.getTotalRxBytes();
mStartTX = TrafficStats.getTotalTxBytes();
if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Uh Oh!");
    alert.setMessage("Your device does not support traffic stat monitoring.");
    alert.show();
} else {
    mHandler.postDelayed(mRunnable, 1000);
}

}

private final Runnable mRunnable = new Runnable() {
public void run() {
    TextView RX = (TextView)findViewById(R.id.RX);      TextView TX = (TextView)findViewById(R.id.TX);

        long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
        RX.setText(Long.toString(rxBytes));
        long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
        TX.setText(Long.toString(txBytes));
        mHandler.postDelayed(mRunnable, 1000);


        final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
        myChronometer.start();



        DisplayWifiState();
        this.registerReceiver(this.myWifiReceiver, new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION));

    }

    private void registerReceiver(BroadcastReceiver myWifiReceiver2,
            IntentFilter intentFilter) {
        // TODO Auto-generated method stub

    }

    private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            NetworkInfo networkInfo = (NetworkInfo) arg1
                    .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                DisplayWifiState();
            }
        }
    };

    public void DisplayWifiState() {

        ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo myNetworkInfo = myConnManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

        if (myNetworkInfo.isConnected()) {

            textSsid.setText(myWifiInfo.getSSID());

            textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " "
                    + WifiInfo.LINK_SPEED_UNITS);
            textRssi.setText(String.valueOf(myWifiInfo.getRssi()));
        } else {
            textSsid.setText("---");

            textSpeed.setText("---");
            textRssi.setText("---");
        };

    // Start service using AlarmManager

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, 10);
        Intent intent = new Intent(Rules.this, LMW.class);
        PendingIntent pintent = PendingIntent.getService(Rules.this, 0, intent,
                0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                100000000 * 1000, pintent);


        // Start 2nd service using AlarmManager

      //  Calendar cal = Calendar.getInstance();
      //  cal.add(Calendar.SECOND, 10);
        Intent intent2 = new Intent(Rules.this, KillTimer.class);
        PendingIntent pintent2 = PendingIntent.getActivity(Rules.this, 0, intent2,
                0);
        AlarmManager alarm2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                30 * 1000, pintent2); // here    

    // click listener for the button to start service
    Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getBaseContext(), LMW.class));            
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);



        }        

});

    // click listener for the button to stop service
    Button btnStop = (Button) findViewById(R.id.button2);
    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getBaseContext(), LMW.class));
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);

        }
    });





}};}

KillTimer.java

   public class KillTimer extends Activity {

    @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.killtimer);
      Toast.makeText(getApplicationContext(), "KillWifi Running!", Toast.LENGTH_SHORT).show();
      WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
      int networkId = wifiManager.getConnectionInfo().getNetworkId();
      wifiManager.removeNetwork(networkId );
      wifiManager.saveConfiguration();

  }}
Bill Florentine
  • 81
  • 1
  • 5
  • 13

2 Answers2

1

Change the PendingIntent in the second alarm. Instead of pintent use pintent2.

Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);
    Intent intent = new Intent(Rules.this, LMW.class);
    PendingIntent pintent = PendingIntent.getService(Rules.this, 0, intent,
            0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            100000000 * 1000, pintent);


    // Start 2nd service using AlarmManager

  //  Calendar cal = Calendar.getInstance();
  //  cal.add(Calendar.SECOND, 10);
    Intent intent2 = new Intent(Rules.this, KillTimer.class);
    PendingIntent pintent2 = PendingIntent.getActivity(Rules.this, 1, intent2,
            0);
    AlarmManager alarm2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            30 * 1000, pintent2); // here
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • Awesome! Thanks! I really appreciate your input... (the 2nd intent still never executes after 30 seconds - I updated my source code above [and shortened it to 10 seconds just for testing purposes] any idea why I still can't launch KillTimer.java?) – Bill Florentine Apr 17 '13 at 09:32
  • @BillFlorentine, try to delay the launch of the second alarm just for testing purpose. And also change the requestCode for the second alarm to 1. See my edit. – Lazy Ninja Apr 17 '13 at 09:41
  • I updated it as suggested - still no execution of KillTimer.java (I added the source code for KillTimer.java above - as well as edited my updated source code with your most recent suggestion) – Bill Florentine Apr 17 '13 at 09:51
  • In your update I find out that KillTimer is an activity. So instead of using PendingIntent.getService() you should use PendindIntent.getActivity(). – Lazy Ninja Apr 17 '13 at 09:54
  • I did (but strangely enough - still no KillTimer ever gets executed!) : ) [thanks again for trying to help me figure this out] – Bill Florentine Apr 17 '13 at 10:05
  • Is there anything else I could possibly try? – Bill Florentine Apr 17 '13 at 10:23
  • 1
    Does the code fall over at any point i.e. have you added the KillTimer activity to your manifest? – confused_at_times Apr 17 '13 at 11:22
  • @confused_at_times, good point! Just assumed he has it in his manifest. Anyway I have working code in my actual project regarding Multi AlarmManager. If you can not figure out the problem, I can share part of my code. – Lazy Ninja Apr 18 '13 at 01:08
0

Please have a try with this code.

// context variable contains your `Context`
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(i = 0; i < 10; ++i)
{
   Intent intent = new Intent(context, OnAlarmReceiver.class);
   // Loop counter `i` is used as a `requestCode`
   PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
   // Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
   mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                SystemClock.elapsedRealtime() + 60000 * i, 
                pendingIntent); 

   intentArray.add(pendingIntent);
}
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31