0

嗨想在我的应用程序中播放警报,所以当我单击开始按钮警报正在播放时,我尝试使用下面的代码使用服务。但我想要的是,相同的活动我首先有两个编辑文本,我有时间 9,第二个编辑文本我已经给了时间 10,所以我必须只在晚上 9 点和晚上 10 点显示通知……任何人都给我一些想法。

    public class DonnsAlarm extends Activity {
/** Called when the activity is first created. */
EditText e1,e2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   e1=(EditText)findViewById(R.id.editText1);
   e2=(EditText)findViewById(R.id.editText2);



    // 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) {
               String s1=e1.getText().toString();
                String s2=e2.getText().toString();

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s1));


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


 PendingIntent pintent1 = PendingIntent.getService(DonnsAlarm.this, 1, intent,
                0);

 cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s2));
  alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                36000 * 1000, pintent1);




        }
    });

    // 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(), Service_class.class));
        }
    });
}
      }
    public class Service_class extends Service {

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
        notifyIntent.setClass(getApplicationContext(), DonnsAlarm.class);

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence from ="It's Time to Play VocCards!";
        CharSequence message = "It's Time to Play VocCards!";

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);


        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();

        Notification notif = new Notification(R.drawable.ic_launcher,
             "It's Time to Play VocCards!", System.currentTimeMillis());
       notif.setLatestEventInfo(this, from, message, contentIntent);
        nm.notify(1, notif);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
  }
4

2 回答 2

0
    btnStart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String s1=e1.getText().toString();
                String s2=e2.getText().toString();

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s1));


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


 PendingIntent pintent1 = PendingIntent.getService(DonnsAlarm.this, 1, intent,
                0);

 cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s2));
  alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                36000 * 1000, pintent1);



            }
        });

上面的代码设置两个报警,一个根据第一个edittext中的值,一个根据另一个

于 2013-06-26T12:59:28.067 回答
0

我看到您的问题是将 EditTexts 的值提供给服务,以便服务知道在哪个小时播放警报。您可以将 EditText 的值放入捆绑包(或意图的附加内容)中,并使用包含有关小时数的数据的意图启动服务。

在您的活动中使用:

btnStart.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        String s1=e1.getText().toString();
        String s2=e2.getText().toString();
        Intent intent = new Intent(getBaseContext(), Service_class.class);
        intent.putExtra("value1", s1);
        intent.putExtra("value2", s2);
        startService(intent);

    }
});

比在你的服务的 onStartCommand 方法中使用这个:

String val1 = intent.getExtra("value1");
String val2 = intent.getExtra("value2");

现在,您的服务拥有价值!

于 2013-06-26T12:51:11.900 回答