• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.example.android.pingme;
16 
17 import android.app.Activity;
18 import android.content.Intent;
19 import android.os.Bundle;
20 import android.view.View;
21 import android.widget.EditText;
22 import android.widget.Toast;
23 
24 public class MainActivity extends Activity {
25 
26     private Intent mServiceIntent;
27 
28     @Override
onCreate(Bundle savedInstanceState)29     public void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(R.layout.activity_main);
32 
33         // Creates an explicit Intent to start the service that constructs and
34         // issues the notification.
35         mServiceIntent = new Intent(getApplicationContext(), PingService.class);
36     }
37 
38     /*
39      * Gets the values the user entered and adds them to the intent that will be
40      * used to launch the IntentService that runs the timer and issues the
41      * notification.
42      */
onPingClick(View v)43     public void onPingClick(View v) {
44         int seconds;
45 
46         // Gets the reminder text the user entered.
47         EditText msgText = (EditText) findViewById(R.id.edit_reminder);
48         String message = msgText.getText().toString();
49 
50         mServiceIntent.putExtra(CommonConstants.EXTRA_MESSAGE, message);
51         mServiceIntent.setAction(CommonConstants.ACTION_PING);
52         Toast.makeText(this, R.string.timer_start, Toast.LENGTH_SHORT).show();
53 
54         // The number of seconds the timer should run.
55         EditText editText = (EditText)findViewById(R.id.edit_seconds);
56         String input = editText.getText().toString();
57 
58         if(input == null || input.trim().equals("")){
59             // If user didn't enter a value, sets to default.
60             seconds = R.string.seconds_default;
61         } else {
62             seconds = Integer.parseInt(input);
63         }
64         int milliseconds = (seconds * 1000);
65         mServiceIntent.putExtra(CommonConstants.EXTRA_TIMER, milliseconds);
66         // Launches IntentService "PingService" to set timer.
67         startService(mServiceIntent);
68     }
69 }
70