Question

following is my code :

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class AlarmMainActivity extends Activity {
    Calendar cal = Calendar.getInstance();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.main);

   Button set = (Button)findViewById(R.id.button1);
   EditText Time = (EditText)findViewById(R.id.time);
   int n =Integer.parseInt(Time.getText().toString());

    //Create an offset from the current time in which the alarm will go off.   
    cal.add(Calendar.SECOND, 10);

    //Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(this, AlarmReceiverActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
        n, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = 
        (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);     
}

}

what happens in this code is this app automatically set alarm of 10 sec at current time. I want that user enter in after how many seconds want. I tried and put in cal.add(Calendar.SECOND, 10); in place of 10. but it is not working .

Was it helpful?

Solution 2

well you can do one thing I've created an another class

Home.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Home extends Activity{

    static int n = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);


        Button go = (Button)findViewById(R.id.button1);
        go.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText sec = (EditText)findViewById(R.id.editText1);
                n = Integer.parseInt(sec.getText().toString());
                Intent go = new Intent("com.nerdwin15.demo.alarmdemo.AlarmMainActivity");
                startActivity(go);
            }
        });
    }

}

AlarmMainActivity.java

public class AlarmMainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.main);

        //Create an offset from the current time in which the alarm will go off.
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, Home.n);

        //Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(this, AlarmReceiverActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
            12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = 
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                pendingIntent);
    }

}

Now you enter your time in home class and it go to another one . make sure to change your LAUNCHER in manifest it should be Home.java

OTHER TIPS

You have to put this int n =Integer.parseInt(Time.getText().toString()); in some Button click event

like below

ib_load.setOnClickListener(new  View.OnClickListener() {

    @Override
    public void onClick(View v) {
    int n =Integer.parseInt(Time.getText().toString());
     cal.add(Calendar.SECOND, n);

    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top