Permalink
Browse files

adding new file paths

  • Loading branch information...
1 parent 322f16d commit b45fc8ea023027c1c841e068c1ba4257ae4b4bba geremy committed Feb 6, 2013
@@ -1,35 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.ForegroundBackgroundDemo"
+ package="com.pubnub.foregroundbackgrounddemo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="1"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
- android:name=".ApplicationContext"
+ android:name="com.pubnub.foregroundbackgrounddemo.ApplicationContext"
android:icon="@drawable/icon"
android:label="@string/app_name">
- <receiver android:name=".BootReceiver">
+ <receiver android:name="com.pubnub.foregroundbackgrounddemo.BootReceiver">
<intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
+ <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
- android:name="com.aimx.androidpubnub.MainActivity"
+ android:name="com.pubnub.foregroundbackgrounddemo.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
- <action android:name="com.aimx.androidpubnub.MESSAGE"/>
+ <action android:name="com.pubnub.foregroundbackgrounddemo.MESSAGE"/>
</intent-filter>
</activity>
- <service android:name=".MessageService"
+ <service android:name="com.pubnub.foregroundbackgrounddemo.MessageService"
android:process=":my_process" android:permission="">
<intent-filter>
- <action android:name="com.aimx.androidpubnub.MessageService" >
+ <action android:name="com.pubnub.foregroundbackgrounddemo.MainActivity.MessageService">
</action>
</intent-filter>
@@ -0,0 +1,66 @@
+package com.pubnub.foregroundbackgrounddemo;
+import android.app.Application;
+import android.content.Context;
+import android.util.Log;
+
+public class ApplicationContext extends Application
+{
+
+ public static boolean isSubscribed() {
+ return subscribed;
+ }
+
+ public static void justSubscribed() {
+ subscribed = true;
+ }
+
+ public static boolean isActivityVisible() {
+ return activityVisible;
+ }
+
+ public static void activityResumed() {
+ activityVisible = true;
+ }
+
+ public static void activityPaused() {
+ activityVisible = false;
+ }
+
+ private static boolean activityVisible;
+ private static boolean subscribed = false;
+
+
+ private static ApplicationContext instance = null;
+ private static Pubnub pubnub = null;
+
+ @Override
+ public void onCreate() {
+ // TODO Auto-generated method stub
+
+ super.onCreate();
+ instance = this;
+ pubnub = new Pubnub("demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY (Cipher key is Optional)
+ true );
+ }
+
+ public static Pubnub getPubnub() {
+ return pubnub;
+ }
+
+ public static void setPubnub(Pubnub pubnub) {
+ ApplicationContext.pubnub = pubnub;
+ }
+
+ public static Context getInstance()
+ {
+ if (null == instance)
+ {
+ instance = new ApplicationContext();
+ }
+
+ return instance;
+ }
+}
@@ -0,0 +1,18 @@
+package com.pubnub.foregroundbackgrounddemo;
+
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+public class BootReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.e("OnBootReceiver", "BootReceiver is Running!");
+ Intent myIntent = new Intent(context, MainActivity.class);
+ myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ context.startActivity(myIntent);
+ }
+}
@@ -0,0 +1,16 @@
+package com.pubnub.foregroundbackgrounddemo;
+
+public interface Callback {
+
+ public abstract boolean subscribeCallback(String channel, Object message);
+
+ public abstract boolean presenceCallback(String channel, Object message);
+
+ public abstract void errorCallback(String channel, Object message);
+
+ public abstract void connectCallback(String channel);
+
+ public abstract void reconnectCallback(String channel);
+
+ public abstract void disconnectCallback(String channel);
+}
@@ -0,0 +1,156 @@
+package com.pubnub.foregroundbackgrounddemo;
+
+import java.util.HashMap;
+import java.util.List;
+
+import android.content.*;
+import android.os.*;
+import android.util.Log;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningAppProcessInfo;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.view.View;
+import android.widget.Button;
+import android.widget.Toast;
+
+
+
+public class MainActivity extends Activity {
+
+ String channel = "string_demo";
+ IntentFilter messageFilter = new IntentFilter("com.pubnub.foregroundbackgrounddemo.MESSAGE");
+ MessageReceiver messageReceiver = new MessageReceiver();
+ Button subscribe = null;
+ Boolean isSubscribed = false;
+
+ Messenger myService = null;
+ boolean isBound;
+
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+
+ subscribe = (Button) findViewById(R.id.subscribe);
+ subscribe.setOnClickListener(new View.OnClickListener() {
+ public void onClick(View v) {
+ subscribe();
+ isSubscribed = true;
+ Toast.makeText(getApplicationContext(), "Subscribed messages will now appear regardless if application is in foreground or background.", Toast.LENGTH_LONG).show();
+ }
+ });
+
+ Intent intent = new Intent("package com.pubnub.foregroundbackgrounddemo.MessageService");
+ bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
+
+ }
+
+ private ServiceConnection myConnection = new ServiceConnection() {
+ public void onServiceConnected(ComponentName className, IBinder service) {
+ myService = new Messenger(service);
+ isBound = true;
+ Log.e("ServiceConnection", "Service Established.");
+ }
+
+ public void onServiceDisconnected(ComponentName className) {
+ myService = null;
+ isBound = false;
+ Log.e("ServiceConnection", "Service Disconnected.");
+ }
+ };
+
+ @Override
+ public void onPause() {
+ super.onPause();
+
+ ApplicationContext.activityPaused();
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+
+ ApplicationContext.activityResumed();
+ }
+
+ @Override
+ public void onRestart() {
+ super.onResume();
+ }
+
+ @Override
+ public void onDestroy() {
+ Log.e("onDestroy", "Destroying PubNub Example app.");
+ if(ApplicationContext.isSubscribed()){
+ unsubscribe();
+ }
+
+ super.onDestroy();
+ }
+
+ private void subscribe(){
+
+ if (!ApplicationContext.isSubscribed()) {
+ registerReceiver(messageReceiver, messageFilter);
+ Intent messageService = new Intent(this, MessageService.class);
+ messageService.putExtra("channel", channel);
+ ApplicationContext.getInstance().startService(messageService);
+ ApplicationContext.justSubscribed();
+ }
+ }
+
+ private void unsubscribe(){
+ killService();
+ try {
+ unregisterReceiver(messageReceiver);
+ } catch (Exception ex){
+ // Do something better
+ }
+ }
+
+ private void killService() {
+ HashMap<String, Object> args = new HashMap<String, Object>();
+ args.put("channel", channel);
+ ApplicationContext.getPubnub().unsubscribe(args);
+
+ Intent messageservice = new Intent(this, MessageService.class);
+ messageservice.putExtra("channel", channel);
+ stopService(messageservice);
+ }
+
+ public class MessageReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+
+ StringBuilder displayText;
+ displayText = new StringBuilder();
+
+ String message = "";
+ String rawData = intent.getStringExtra("message");
+
+ try {
+ message = new JSONTokener(rawData).nextValue().toString();
+ } catch (JSONException e) {
+ message = (String)rawData;
+ }
+
+ if (message != null) {
+ if (ApplicationContext.isActivityVisible()) {
+ displayText.append("Foreground Message received: ").append(message);
+ } else {
+ displayText.append("Background Message received: ").append(message);
+ }
+
+ Toast.makeText(getApplicationContext(), displayText.toString(), Toast.LENGTH_LONG).show();
+
+ }
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit b45fc8e

Please sign in to comment.