我有两个应用程序 APP1 (包名-:com.abc.q)和 APP2 (Package name -:com.xyz.y)在APP1中有一个活动将打开另一个APP2活动,该活动具有来自parse.com .I的通知--实际上,每当APP2收到该通知时,当APP2从获得通知时,它应该在APP1中显示"1“,在# an 20#app1中显示APP2红色椭圆通知。
APP1酸性
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) {
// We found the activity now start the activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
// Bring user to the market or let them choose an 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 is null. Not subscribing to 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);
}
}当DBAdapter.java中有一个新的通知时,它应该在gkk.java中显示其他软件包app活动的通知椭圆。
发布于 2016-01-16 08:33:59
这里是您的GKK.java文件内容应该是。请注意,我创建新文件只是为了检查流。您可以在您希望接收通知的文件中替换该文件中的内容。请注意,在注释中,我使用了sendBroadcast而不是LocalBroadcastManager,因为您要跨不同的应用程序发送通知。因此,只为了检查是否一切正常,或者现在从终端运行此命令。
亚行空壳广播in.ashish29agre.stackoverflow.retrofit.appnotify.NEW_ROW_INSERTED -a
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 successfully with 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 this is in terminal to check notification\nadb shell am broadcast -a in.ashish29agre.stackoverflow.retrofit.appnotify.NEW_ROW_INSERTED" />
</RelativeLayout>这是你最新更新的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);
}
}
}https://stackoverflow.com/questions/34824042
复制相似问题