I've noticed when trying to share text from other Apps and choosing Facebook to save, Facebook's Receiving Activity doesn't open (At least it's that fast, maybe), it only shows a toast confirmation message.
This is how I tried doing it
private void welcomeSharedData(){
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
saveTextAsNote();
toastMessage();
finish();
}
}
}
My problem with the above is, my App shows a flashing screen of the Activity then goes back to the Calling App.
I want it to be quiet and only reply with a confirmation message on a toast.
Related
i want to make a swiggy type app. i have integrated gmail login in my login screen and it changes to dashboard activity on success but when i click on account tab it goes back to dashboard screen instead of account activity. pls help......
login screen
dashboard
if you want any portion of code just comment and i will update
//login screen part
//check if already signed in using google
account = GoogleSignIn.getLastSignedInAccount(this);
if(account!=null) {
finish();
Intent intent = new Intent(this, DashboardActivity.class);
startActivity(intent);
return;
}
//onclicklistener added
//method
private void googleSignin() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
onActivity result(//params provided){
if(googleLogin){
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
googleLogin = false; //set to false so that it can be set true again if login is actually successful
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
sessionManager.setLogin(true);
googleLogin = true;
Intent intent = new Intent(this,DashboardActivity.class);
intent.putExtra("googleLogin", googleLogin);
startActivity(intent);
finish();
}
//Dashboard part
Intent i = new Intent(DashboardActivity.this ,MyAccountActivity.class);
i.putExtra("googleLogin", googleLogin);
startActivity(i);
//myaccount part
if(googleLogin){
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
if (acct != null) {
String personName = acct.getDisplayName();
//System.out.println(personName); working fine
account_name.setText(personName);
String personEmail = acct.getEmail();
//System.out.println(personEmail); fine
account_email.setText(personEmail);
account_mobile.setText("+91 1234567890");
// System.out.println(googleLogin);
// System.out.println(fbLogin);
}
}
solved this problem.
the issue was gmail was not able to provide account details for use when i tried to display them in MY ACCOUNT activity that i integrated in my app, thats why it kept on getting crashed. the code was correct thats why it wasnt giving any error i even tried logging it. there it showed correct details but still it wasnt displaying the result in text views.
so what i did was use shared preferences. everytime the user logs in using gmail, the details are stored in them and i retrieve it in MY ACCOUNT activity and yes it displayed the results correctly.
let me know if you need any more help.
I'm trying to develop a simple app, which receives text from other android Apps and then open a browser.
I have implemented it as described in the documentation here:
https://developer.android.com/training/sharing/receive.html
It works, but only once.
The first time a text is shared from an other App, the browser is opened correctly.
But the second time only my app is opened, but not the browser.
What could be the reason for this?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the intent that started this activity
Intent intent = getIntent();
// Get the action of the intent
String action = intent.getAction();
// Get the type of intent (Text or Image)
String type = intent.getType();
// When Intent's action is 'ACTION+SEND' and Type is not null
if (Intent.ACTION_SEND.equals(action) && type != null) {
// When tyoe is 'text/plain'
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
}
}
}
private void handleSendText(Intent intent) {
// Get the text from intent
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
openBrowser(sharedText);
}
}
private void openBrowser(String text) {
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com/api.php?text=" + text));
startActivity(browserIntent);
}
}
openBrowser method is called from the handleSendText method witch it is on the onCreate method , second time you open your app ( If you didnt press the back button ) your app is already created ! so the code will never execute.
Please check the life cycle of an android activity below
You may edit your code and call the openBrowser method on the onResume method or just make a button to call the method openBrowser Oncliking the button.
I am working on a messaging app, it sends user notification when he is on a different activtyon my app or is on another app but if the user is on MessagingActivity.java it just updates the chat history and does not send any notifications which is perfectly fine, but the problem arises when the user is on MessagingActivity.java meanwhile an email or something else happen user leaves the MessagingActivity.java open and checks that app if in the meantime a message comes user does not receive any notifications
public void parseRequest(Bundle extras) {
if (extras.containsKey("for") && extras.containsKey("recipientID")) {
if (Integer.parseInt(extras.getString("recipientID")) == M.getID(this)) {
switch (extras.getString("for")) {
case "chat":
if (isRunning("MessagingActivity")) {
Intent intent = new Intent("update_messages_list");
intent.putExtra("data", extras);
sendBroadcast(intent);
} else {
Intent resultIntent = new Intent(this, MessagingActivity.class);
resultIntent.putExtra("conversationID", Integer.parseInt(extras.getString("conversationID")));
resultIntent.putExtra("recipientID", Integer.parseInt(extras.getString("ownerID")));
M.showNotification(getApplicationContext(), resultIntent,
extras.getString("ownerUsername"),
extras.getString("message"),
Integer.parseInt(extras.getString("conversationID")));
}
Let me know how you are checking that your MessageActivity is Running i.e. functioning of isRunning("MessagingActivity") method. If you are setting any global boolean variable for checking this and making isRunning value false in onDestroy() method of that activity then, according to life cycle of Activity it is not called until your activity is finished i.e. in your case user just switching from MessageActivity to Mail .
I am by no means an expert, but you could just set a boolean variable by overriding the Activity's onPause() and onResume() events.
Simply set msgActivityActive to true in onResume(), false in onPause(), and change your call to:
if (isRunning("MessagingActivity") && msgActivityActive)
My Application required bluetooth connectivity. And in the first phase I am trying to open up the standard Activity "Bluetooth Device Picker" to help user scan for new device or chose a device from the list.
The problem is that I am unable to get any working example for the bluetooth device picker. The task is simple. To start an Activity with Intent "android.bluetooth.devicepicker.action.LAUNCH"
And the device picker is opening without any problem.
But the device picker requires four extras and I am unable to figure out the exact parameters for two of the extras listed below.
.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.extreme.controlcenter"
.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.extreme.controlcenter.WelcomeActivity")
What I thought the parameters should be that
*"android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE"*
should have the name of my package, so I passed that only. That is "com.extreme.controlcenter"
The second should be the name of the component that must receive the broadcast that is done after a device is selected. Here I tried putting the name of the class that has the onReceive() function.
But the problem is that the onReceive() function is NOT getting called when a device is picked in device picker!
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Device Selected on Device Picker
if("android.bluetooth.devicepicker.action.DEVICE_SELECTED".equals(action)) {
//context.unregisterReceiver(this);
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show();
String MAC = device.getAddress();
//Log.d("my", MAC);
Intent intent2 = new Intent(WelcomeActivity.this, ControlActivity.class);
intent2.putExtra(EXTRA_DEVICE_ADDRESS, MAC);
startActivity(intent2);
}
};
I have created an Intent filter and registered a receive in the onCreate() of the main Activity
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED");
registerReceiver(mReceiver, filter);
One thing is that if I don't provide those two extras, the Broadcast event is received successfully. But that code only runs on my TAB, but same is crashing in cell phone. So I think providing those two extras are mandatory.
Thanks in Advance !
"com.extreme.controlcenter.WelcomeActivity" in your EXTRAs needs to be a BroadcastReceiver class such as MyBroadcastReceiver.class.getName(). I also have it declared in the manifest inside the tags
I have an activity which gets called by 2 intents, one after a simple menu-selection and the other way by a intent after a deletion of an item in a database. However, I wanted to display in the called activity a little Toast, but only when it's opened through the intent of the deletion. I thought of following solution
public void intentCheck(){
Log.d("ShowActivity","intentCheck() called");
Bundle extras = getIntent().getExtras();
if (extras != null){
String check = extras.getString("AdvancedViewActivityCall");
if(check == "calling"){
Log.d("ShowActivity","delete-intent succeeded");
Toast success = new Toast(ShowActivity.this);
success.makeText(ShowActivity.this, "Deletion succeded", Toast.LENGTH_LONG);
}
}
but it doesn't work... somehow, no toast gets displayed.
edit:// i applied success.show(); now, but now i get a RunetimeException O.o ( http://pastebin.com/Th3NY5d0 )
edit: SOLUTION: Toast.makeText(context, text, duration).show(); //seems to be the "static way", which eclipse proposed
Have you tried if ("calling".equals(check)) instead of if(check == "calling") ?
EDIT:
try Toast.makeText(context, text, duration).show();
you have to call show method for toast until otherwise toast will not display.
success.show();