java - Pass value of one application to other package application in background like facebook & facebook messenger app -


i have 2 app app1(package name-: com.abc.q) , app2(package name -:com.xyz.y) in app1 there activity open app2 activity has notification comes parse.com .i want whenever app2 gets notification should show "1" in app1 in small red oval.in gist when app2 gets notification parse.com should show red oval notification in app1 in activity have decided.

app1 acivity

1.)gkk.java

public class gkk extends activity {     mediaplayer oursong;     private static final string tag = "gkk";     button button;private progressdialog pdialog;     private webview webview;     private webview webview1;     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         this.requestwindowfeature(window.feature_no_title);           setcontentview(r.layout.lgin);           webview = (webview) findviewbyid(r.id.webview1);         webview1 = (webview) findviewbyid(r.id.webview11);         webview.getsettings().setjavascriptenabled(true);         webview.setwebviewclient(new insidewebviewclient());         webview1.getsettings().setjavascriptenabled(true);         webview1.setwebviewclient(new mywebclient());          webview.loadurl("www.xyz.comm");          webview1.loadurl("www.xyz.comm");           oursong=mediaplayer.create(gkk.this, r.raw.rollver);         floatingactionbutton fabbutton = new floatingactionbutton.builder(this)          .withdrawable(getresources().getdrawable(r.drawable.float_button))         .withbuttoncolor(color.white)          .withgravity(gravity.bottom | gravity.right)          .withmargins(0, 0, 12, 12)          //.withbuttonsize(180)         .create();         fabbutton.setonclicklistener(new onclicklistener() {             public void onclick(view arg0) {                  //intent intent = new intent(gkk.this,st.class);                 intent intent = new intent(gkk.this,loginactivity.class);                 startactivity(intent);                 overridependingtransition( r.anim.down, r.anim.toup );             }         });           floatingactionbutton fabbutton2 = new floatingactionbutton.builder(this)          .withdrawable(getresources().getdrawable(r.drawable.n19))         .withbuttoncolor(color.transparent)         .withgravity(gravity.bottom | gravity.right)         .withmargins(0, 0, 12, 85)          .create();          fabbutton2.setonclicklistener(new onclicklistener() {             public void onclick(view arg0) {                 intent intent = getpackagemanager().getlaunchintentforpackage("com.xyz.y");                 if (intent != null) {                     // found activity start activity                     intent.addflags(intent.flag_activity_new_task);                     startactivity(intent);                 } else {                     // bring user market or let them choose app?                     intent = new intent(intent.action_view);                     intent.addflags(intent.flag_activity_new_task);                     intent.setdata(uri.parse("market://details?id=" + "com.package.name"));                     startactivity(intent);                 }              }         });           webview.setbackgroundcolor(0x00000000);         webview1.setbackgroundcolor(0x00000000);         pdialog = new progressdialog(this);         // showing progress dialog before making http request         pdialog.setmessage("fetching notice's..");         pdialog.show();          addlisteneronbutton();         webview.setwebviewclient(new insidewebviewclient() {             public void onreceivederror(webview view, int errorcode, string description, string failingurl) {                 webview.loadurl("file:///android_asset/error1.html");                 pdialog.dismiss();             }         });          webview1.setwebviewclient(new mywebclient() {             public void onreceivederror(webview view, int errorcode, string description, string failingurl) {                 webview1.loadurl("file:///android_asset/notification.html");                 pdialog.dismiss();             }         });     }      public class insidewebviewclient extends webviewclient {         @override         public boolean shouldoverrideurlloading(webview view, string url) {             if (url.endswith(".pdf"))                  startactivity(new intent(intent.action_view, uri.parse(url)));              return true;         }         public void onpagefinished(webview view, string url)         {             log.i(tag, "finished loading url: " +url);              if (pdialog.isshowing()) { pdialog.dismiss(); } }  }      class mywebclient extends webviewclient     {         @override         public void onpagestarted(webview view, string url, bitmap favicon) {             // todo auto-generated method stub             super.onpagestarted(view, url, favicon);         }          @override         public boolean shouldoverrideurlloading(webview view, string url) {             // todo auto-generated method stub              view.loadurl(url);             return true;          }}   public void onlunchanotherapp() {          final string apppackagename = getapplicationcontext().getpackagename();          intent intent = getpackagemanager().getlaunchintentforpackage(apppackagename);         if (intent != null) {              intent.addflags(intent.flag_activity_new_task);             startactivity(intent);          } else {              ongotoanotherinappstore(intent, apppackagename);          }      }      public void ongotoanotherinappstore(intent intent, string apppackagename) {          try {              intent = new intent(intent.action_view);             intent.addflags(intent.flag_activity_new_task);             intent.setdata(uri.parse("https://play.google.com/store/apps/details?id=com.cc.vv=en" + apppackagename));             startactivity(intent);          } catch (android.content.activitynotfoundexception anfe) {              intent = new intent(intent.action_view);             intent.addflags(intent.flag_activity_new_task);             intent.setdata(uri.parse("http://play.google.com/store/apps/details?id=" + apppackagename));             startactivity(intent);          }      }       public void addlisteneronbutton() {          final context context = this;            button tut4= (button) findviewbyid(r.id.button18);          }    } 

app2 activity

2.)dbadapter.java

public class dbadapter {       public static final string key_rowid = "_id";     public static final string key_date = "date";     public static final string key_title = "title";     public static final string key_message = "message";       public static final string[] all_keys = new string[] { key_rowid,             key_date, key_title, key_message };       public static final string database_name = "dbnotif";     public static final string table_name = "notif";     public static final int database_version = 1;          private static final string database_create_sql = "create table "             + table_name + " (" + key_rowid             + " integer primary key autoincrement, " + key_date             + " text not null, " + key_title + " text not null, " + key_message             + " text not null" + ");";       private final context context;       private databasehelper mydbhelper;       private sqlitedatabase db;       public dbadapter(context ctx) {         this.context = ctx;         mydbhelper = new databasehelper(context);     }       public dbadapter open() {         db = mydbhelper.getwritabledatabase();         return this;     }       public void close() {         mydbhelper.close();     }      // insert data      public long insertnotif(string date, string title, string message) {         long result = 0;          open();         contentvalues cv = new contentvalues();         cv.put(key_date, date);         cv.put(key_title, title);         cv.put(key_message, message);          result = db.insert(table_name, null, cv);         close();          return result;     }        public cursor getallnotif() {         open();         cursor c = db.query(true, table_name, all_keys, null, null, null,                 null, key_date + " desc", null, null);         if (c != null) {             c.movetofirst();         }         close();          return c;     }       private static class databasehelper extends sqliteopenhelper {         databasehelper(context context) {             super(context, database_name, null, database_version);         }           @override         public void oncreate(sqlitedatabase _db) {             _db.execsql(database_create_sql);         }           @override         public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) {               _db.execsql("drop table if exists " + table_name);               oncreate(_db);         }     } } 

3.)lgin.xml

<?xml version="1.0" encoding="utf-8"?>     <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="#ffffff"     android:orientation="vertical"     android:padding="0dip" >            <webview             android:id="@+id/webview1"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:layout_weight="1.0"             android:background="#ffffff"             android:layout_above="@+id/button1"             android:layout_below="@+id/button18" >         </webview>        <button         android:id="@+id/button18"         android:layout_width="match_parent"         android:layout_height="69dp"         android:layout_alignparentleft="true"         android:layout_alignparentright="true"         android:layout_alignparenttop="true"         android:background="#333366"         android:gravity="center"         android:paddingbottom="37dip"         android:text="xyz"         android:textcolor="#ffffff"         android:textsize="19dip"         android:textstyle="bold" />      <webview         android:id="@+id/webview11"         android:layout_width="match_parent"         android:layout_height="32dp"         android:layout_alignbottom="@+id/button18"         android:layout_alignparentright="true"         android:layout_weight="1"         android:background="#333366" />      <imageview         android:id="@+id/webview11qwd"         android:layout_width="match_parent"         android:layout_height="3dp"         android:layout_alignparentleft="true"         android:layout_below="@+id/button18"         android:layout_weight="1"         android:background="@drawable/card_depth" />  </relativelayout> 

4.)main.java

public class main  extends appcompatactivity {      private static string tag = main.class.getsimplename();     private toolbar mtoolbar;     private dbadapter db;     private listview list;     private listview listview;     private list<message> listmessages = new arraylist<>();     private messageadapter adapter;     private prefmanager pref;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          listview = (listview) findviewbyid(r.id.list_view);         mtoolbar = (toolbar) findviewbyid(r.id.toolbar);        setsupportactionbar(mtoolbar);         getsupportactionbar().setdisplayshowhomeenabled(true);          adapter = new messageadapter(this);         pref = new prefmanager(getapplicationcontext());          listview.setadapter(adapter);          intent intent = getintent();          string email = pref.getemail();          if (email != null) {             parseutils.subscribewithemail(pref.getemail());         }else{             log.e(tag, "email null. not subscribing parse!");         }         initview();         setnotif();     }     private void initview() {                  this.db = new dbadapter(this);                  this.list = (listview) findviewbyid(r.id.list_view);     }       private void setnotif() {                  cursor cnotif = db.getallnotif();          if (cnotif.getcount() > 0) {                      string[] data = new string[] { dbadapter.key_rowid, dbadapter.key_date, dbadapter.key_title, dbadapter.key_message };              int[] view = new int[] { r.id._id, r.id.date, r.id.title, r.id.message };             simplecursoradapter adapter = new simplecursoradapter(this, r.layout.list_row, cnotif,                     data, view, simplecursoradapter.flag_register_content_observer);             this.list.setadapter(adapter);         }     }     @override     protected void onnewintent(intent intent) {         super.onnewintent(intent);         string message = intent.getstringextra("message");          message m = new message(message, system.currenttimemillis());         listmessages.add(0, m);         adapter.notifydatasetchanged();     }      private class messageadapter extends baseadapter {          layoutinflater inflater;          public messageadapter(activity activity) {             inflater = (layoutinflater) activity.getsystemservice(context.layout_inflater_service);         }          @override         public int getcount() {             return listmessages.size();         }          @override         public object getitem(int position) {             return listmessages.get(position);         }          @override         public long getitemid(int position) {             return position;         }          @override         public view getview(int position, view convertview, viewgroup parent) {             view view = convertview;             if (view == null) {                 view = inflater.inflate(r.layout.list_row, null);             }              textview txtmessage = (textview) view.findviewbyid(r.id.message);             textview txttimestamp = (textview) view.findviewbyid(r.id.timestamp);              message message = listmessages.get(position);             txtmessage.settext(message.getmessage());              charsequence ago = dateutils.getrelativetimespanstring(message.gettimestamp(), system.currenttimemillis(),                     0l, dateutils.format_abbrev_all);              txttimestamp.settext(string.valueof(ago));              return view;         }     }      @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         int id = item.getitemid();           if (id == r.id.action_logout) {             pref.logout();             intent intent = new intent(mainactivity.this, loginactivity.class);             intent.setflags(intent.flag_activity_new_task | intent.flag_activity_clear_task);             startactivity(intent);             finish();             return true;         }          return super.onoptionsitemselected(item);     } } 

when there new notification in dbadapter.java should show notification oval in gkk.java of other package app activity.

here content of gkk.java file should be. note created new file check flow. can replace content file inside file wish receive notification. note comments used sendbroadcast instead of localbroadcastmanager because sending notification across different application. check whether working fine or run command terminal

adb shell broadcast -a in.ashish29agre.stackoverflow.retrofit.appnotify.new_row_inserted

  package in.ashish29agre.notificationreceverapp;  import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.widget.textview;  public class mainactivity extends appcompatactivity {      public static final string notification_action = "in.ashish29agre.stackoverflow.retrofit.appnotify.new_row_inserted";     private notificationbroadcastreceiver receiver;     private intentfilter filter;     private textview notificationstatustv;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         receiver = new notificationbroadcastreceiver();         filter = new intentfilter(notification_action);         notificationstatustv = (textview) findviewbyid(r.id.notification_status_tv);     }      @override     protected void onresume() {         super.onresume();         registerreceiver(receiver, filter);     }      @override     protected void onpause() {         super.onpause();         unregisterreceiver(receiver);     }      private class notificationbroadcastreceiver extends broadcastreceiver {          @override         public void onreceive(context context, intent intent) {              if (intent != null && intent.getaction() != null && intent.getaction().equals(notification_action)) {                 if (intent.hasextra("value")) {                     notificationstatustv.settext("" + intent.getintextra("value", -1));                 } else {                     notificationstatustv.settext("notification received no data");                 }             }         }     } } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context="in.ashish29agre.notificationreceverapp.mainactivity">      <textview         android:id="@+id/notification_status_tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="tye in terminal check notification\nadb shell broadcast -a in.ashish29agre.stackoverflow.retrofit.appnotify.new_row_inserted" /> </relativelayout> 

this new updated dbadapter.java

package in.ashish29agre.stackoverflow.retrofit.appnotify;  import android.content.contentvalues; import android.content.context; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log;  public class dbadapter {      public static final string notification_action = "in.ashish29agre.stackoverflow.retrofit.appnotify.new_row_inserted";      public static final string key_rowid = "_id";     public static final string key_date = "date";     public static final string key_title = "title";     public static final string key_message = "message";       public static final string[] all_keys = new string[] { key_rowid,             key_date, key_title, key_message };       public static final string database_name = "dbnotif";     public static final string table_name = "notif";     public static final int database_version = 1;          private static final string database_create_sql = "create table "             + table_name + " (" + key_rowid             + " integer primary key autoincrement, " + key_date             + " text not null, " + key_title + " text not null, " + key_message             + " text not null" + ");";       private final context context;       private databasehelper mydbhelper;       private sqlitedatabase db;       public dbadapter(context ctx) {         this.context = ctx;         mydbhelper = new databasehelper(context);     }       public dbadapter open() {         db = mydbhelper.getwritabledatabase();         return this;     }       public void close() {         mydbhelper.close();     }      // insert data      public long insertnotif(string date, string title, string message) {         long result = 0;          open();         contentvalues cv = new contentvalues();         cv.put(key_date, date);         cv.put(key_title, title);         cv.put(key_message, message);          result = db.insert(table_name, null, cv);         close();          intent notificationintent = new intent(notification_action);         notificationintent.putextra("value", 1);         context.sendbroadcast(notificationintent);         log.d("notification", "broadcast sent");         return result;     }        public cursor getallnotif() {         open();         cursor c = db.query(true, table_name, all_keys, null, null, null,                 null, key_date + " desc", null, null);         if (c != null) {             c.movetofirst();         }         close();          return c;     }       private static class databasehelper extends sqliteopenhelper {         databasehelper(context context) {             super(context, database_name, null, database_version);         }           @override         public void oncreate(sqlitedatabase _db) {             _db.execsql(database_create_sql);         }           @override         public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) {               _db.execsql("drop table if exists " + table_name);               oncreate(_db);         }     } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -