Advertisement
Guest User

PebMsg Source Code

a guest
Apr 3rd, 2013
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. //Here is the source code for my app, PebMsg.  Search it in the google play store.
  2. //Feel free to do anything with this code.
  3. //The most useful bit will be the sendMsg method, a super easy way to send messages to pebble
  4. //Good luck!
  5. //Created by Jonathan Davis
  6. //jonnysdavis|at|gmail|dot|com
  7.  
  8. package com.example.pebmsg;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import org.json.JSONArray;
  12. import org.json.JSONObject;
  13. import android.os.Bundle;
  14. import android.app.Activity;
  15. import android.content.Context;
  16. import android.content.Intent;
  17. import android.view.Gravity;
  18. import android.view.Menu;
  19. import android.view.View;
  20. import android.view.View.OnClickListener;
  21. import android.widget.Button;
  22. import android.widget.EditText;
  23. import android.widget.Toast;
  24. import co.exampl.pebmsg.R;
  25.  
  26. public class MainActivity extends Activity {
  27.     public final static String PEBBLE_ALERT = "PEBBLE_ALERT";
  28.     String title, body;
  29.  
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.         Button Send = (Button) findViewById(R.id.button1);
  35.         //If they click the button, start the message send
  36.         Send.setOnClickListener(new OnClickListener() {
  37.             public void onClick(View v) {
  38.                 //Method GimmeOne is the first step (at the bottom of the code)
  39.                 GimmeOne();
  40.             }
  41.         });
  42.     }
  43.  
  44.     @Override
  45.     public boolean onCreateOptionsMenu(Menu menu) {
  46.         // Inflate the menu; this adds items to the action bar if it is present.
  47.         getMenuInflater().inflate(R.menu.main, menu);
  48.         return true;
  49.  
  50.     }
  51.  
  52.     public void sendMsg(String name, String text) {
  53.         //Name = Header text, text = body text
  54.         //I copied this code right of off the pebble website
  55.         //Be sure to declare || public final static String PEBBLE_ALERT = "PEBBLE_ALERT"; || somewhere
  56.         final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
  57.         final Map<String, Object> data = new HashMap<String, Object>();
  58.         //The title text is set to name
  59.         data.put("title", name);
  60.         //Body text set to body text
  61.         data.put("body", text);
  62.         final JSONObject jsonData = new JSONObject(data);
  63.         final String notificationData = new JSONArray().put(jsonData)
  64.                 .toString();
  65.         i.putExtra("messageType", PEBBLE_ALERT);
  66.         i.putExtra("sender", "MyAndroidApp");
  67.         i.putExtra("notificationData", notificationData);
  68.         //Sends the data
  69.         sendBroadcast(i);
  70.     }
  71.     public void Delay() {
  72.         int inputnum;
  73.         //Get an EditText variable for the delay field
  74.         EditText editText = (EditText) findViewById(R.id.editText3);
  75.         //Check if blank, if it is no delay.
  76.         if (editText.getText().toString().equals("")) {
  77.             inputnum=0;
  78.         }
  79.         else {
  80.         String temp = editText.getText().toString();
  81.         inputnum = Integer.parseInt(temp);
  82.         }
  83.         try {
  84.             //sleep for input * 1000 to convert milliseconds to seconds
  85.             Thread.sleep(inputnum*1000);
  86.         } catch (InterruptedException e) {
  87.             // TODO Auto-generated catch block
  88.             e.printStackTrace();
  89.         }
  90.     }
  91.     public void GimmeOne() {
  92.         //Get an EditText variable for the head field
  93.         EditText editText = (EditText) findViewById(R.id.editText1);
  94.         //Check if head field is blank, otherwise get input
  95.         if (editText.getText().toString().equals("")) {
  96.             title = "[blank]";
  97.         } else {
  98.             title = editText.getText().toString();
  99.         }
  100.         //Same as above, but for the body
  101.         EditText editText2 = (EditText) findViewById(R.id.editText2);
  102.         if (editText2.getText().toString().equals("")) {
  103.             body = "[blank]";
  104.         } else {
  105.             body = (editText2.getText().toString());
  106.         }
  107.         //Adds a delay before sending, if none then their will be no delay
  108.         Delay();
  109.         //Pushes the message to the pebble
  110.         sendMsg(title, body);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement