I have image steganography app for android. The main function of this app have worked well, such as encoding, decoding, and save images in phone storage.
But, I want a picture that has been inserted a secret message can be shared with other app like Whatsapp, Facebook, Twitter. After I try to make the share button and click the button, I was not given a choice application where I will share those images.
However, I was directed to the MMS message. although the MMS message can work well, but I want to be able to choose the app where I will share images.
This is a part of encoding code where assosiated with share button :
Button buttonShare = (Button) findViewById(R.id.share);
buttonShare.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
closeContextMenu();
closeOptionsMenu();
progressBar=new MobiProgressBar(EncodeActivity.this);
progressBar.setMax(100);
progressBar.setMessage(context.getString(R.string.encoding));
progressBar.show();
Thread tt = new Thread(new Runnable() {
public void run() {
Uri uri= encode();
ShareIntent share=new ShareIntent(uri,EncodeActivity.this);
progressBar.dismiss();
share.send();
}
});
tt.start();
}
});
And this code for share method :
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class ShareIntent {
private Uri uri;
private final Context context;
public Uri getUri() {
return uri;
}
public void setUri(Uri uri) {
this.uri = uri;
}
public ShareIntent(Uri uri,Context context) {
this.uri = uri;
this.context=context;
}
public void send()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,uri);
shareIntent.setType("image/jpeg");
context.startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
}
Thank you!!
Related
This question already has an answer here:
Firebase Have to upload image twice to get it to display
(1 answer)
Closed 1 year ago.
I'm creating an app for my school project, my coding is not that advanced so I apologise in advance for my bad coding. So I'm trying to get the download Uri, so once the user completes the form with the picture, I can upload the details to realtime database to be used in my other parts of my app.
//I use this code below, I got it from the Firebase documents//
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// picUri is a globally declared string and I use it to set my database values,
picUri=(uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
I believe it has to do with the fact that Firebase method is asynchronous? So the uri.toString() doesnt get parsed into the global string variable? Because I've been playing around and I realised that when I do the .setValue(object) to write my object to realtime database, the uri does get captured, but that would be too messy and then I wouldnt be able to capture the other values. Ive read around that I need to write a callback, theres alot of guides out there that I've really confused myself here.
Could someone people direct me to a guide or a youtube tutorial on how to understand callbacks and implement a callback to get this value , or issit something else that I'm doing wrong, please advice , thank you.
The photos and the other parameters are being recorded succesfully, its only the picture's uri that I'm unable to capture. (I've temporarily replaced the categoryinput with picUri to run tests easier)
I'm a new user so i cant upload pics but the pic for database and layout is:
https://i.ibb.co/YbdSzwB/databasestack.png
https://i.ibb.co/SVG2zxL/stackoverflow.png
I'll input my entire code just for info
package com.example.giventake;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.*;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.IOException;
public class ListPage extends AppCompatActivity {
/*Declare all variables I'll be using*/
private EditText title, category, description;
private ImageView image, image2, image3;
private FirebaseAuth mAuth;
private FirebaseStorage storage;
private StorageReference storageReference;
private FirebaseDatabase rootNode;
private DatabaseReference referenceItems;
private Uri filePath;
private Uri filePath2;
private Uri filePath3;
private final int PICK_IMAGE_REQUEST = 1;
private final int PICK_IMAGE2_REQUEST = 2;
private final int PICK_IMAGE3_REQUEST = 3;
String picUri;
/*Declare all variables I'll be using*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
/*Initialise all instances of variables*/
title = (EditText) findViewById(R.id.editTextItemTitle);
category = (EditText) findViewById(R.id.editTextItemCategory);
description = (EditText) findViewById(R.id.editTextItemDescription);
image = (ImageView) findViewById(R.id.imgViewPic);
image2 = (ImageView) findViewById(R.id.imgViewPic2);
image3 = (ImageView) findViewById(R.id.imgViewPic3);
image.setImageResource(R.drawable.ic_baseline_list_24);
image2.setImageResource(R.drawable.ic_baseline_list_24);
image3.setImageResource(R.drawable.ic_baseline_list_24);
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
mAuth = FirebaseAuth.getInstance();
rootNode = FirebaseDatabase.getInstance();
referenceItems = rootNode.getReference("Items");
/*Initialise all instances of variables*/
}
/*This is to upload images from your phones direction*/
public void onFirstPicClick(View view) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select Picture"), PICK_IMAGE_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*This is to upload images from your phones direction*/
public void onSubmitClicked(View view) {
//Fetching data
String titleInput = title.getText().toString().trim();
//String categoryInput = category.getText().toString().trim();
String descriptionInput = description.getText().toString().trim();
String itemId = referenceItems.child(mAuth.getCurrentUser().getUid()).push().getKey();
referenceItems = referenceItems.child(mAuth.getCurrentUser().getUid()).child(itemId);
if (filePath != null) {
StorageReference ref = storageReference.child("Images").child(mAuth.getCurrentUser().getUid()).child(itemId).child("Pic1");
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Toast.makeText(ListPage.this, "Uploaded", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(ListPage.this, "Picture upload failed" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
}
});
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
picUri=(uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
}
//ItemsHelperClass item = new ItemsHelperClass(titleInput, categoryInput, descriptionInput, itemId);
ItemsHelperClass item = new ItemsHelperClass(titleInput,picUri,descriptionInput,itemId);
referenceItems.setValue(item).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Intent i = new Intent(ListPage.this, MainActivity.class);
startActivity(i);
finish();
// getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
} else {
//If it fails to add
Toast.makeText(ListPage.this, "Something went wrong, please try again", Toast.LENGTH_LONG).show();
}
}
}
);
}
}
The Java Firebase Android SDK makes heavy use of Task objects (which are similar to JavaScript Promise objects) where you can chain a number of actions together that need to interact with asynchronous APIs.
A Task object, has many methods to attach listeners, the main ones you'll encounter are:
addOnSuccessListener - When the task completes successfully, run the given code.
addOnFailureListener - When the task fails, run the given code.
addOnCompleteListener - When the task completes (either successfully or has failed), run the given code.
onSuccessTask - When the task completes successfully, start another task.
continueWith - When the task completes (either successfully or has failed), start another task.
Sometimes if you are dealing with multiple tasks at a time, you'll need to make use of methods on the Tasks utility class.
For what you are trying to do, here is a list of the steps you need to take once the user clicks the submit button:
Get the values of any inputs (title, description, category, image file path, etc)
Validate the inputs (make sure each has a value, no invalid characters, etc) and if invalid, show an error.
Create the private references you need: a DatabaseReference and a StorageReference
Upload the image file
If the upload was successful, get a download URL
If getting a download URL was successful, save information to the database.
If any of the tasks in step 4, 5, or 6 fail, show an error.
As this is a school project, I'm not going to dive in any further. Study the links above, and you should be able to combine that knowledge with these steps to find the solution.
However, I will point out this bug that will get you into hot water:
String itemId = referenceItems.child(mAuth.getCurrentUser().getUid()).push().getKey();
referenceItems = referenceItems.child(mAuth.getCurrentUser().getUid()).child(itemId);
You assign to referenceItems here which messes up your code the next time you hit submit and will quickly make your database look like:
{
"Items": {
"-MYu4i_5q-GUETw4WV1i": {
"title": "my first image",
"uri": "https://firebasestorage.googleapis.com/...",
"description": "some description",
"category": "some category",
"-MYu5jNs2MMJJGB2L7jJ": {
"title": "my second image",
"uri": "https://firebasestorage.googleapis.com/...",
"description": "some description",
"category": "some category",
"-MYu72gLxsi4M6J9aBAr": {
"title": "my third image",
"uri": "https://firebasestorage.googleapis.com/...",
"description": "some description",
"category": "some category",
"-MYu78gFeoWhV6OSiF9I": ...
}
}
}
}
}
or, if you had a syntax error rather than a successful upload, you might end up with:
{
"Items": {
"-MYu4i_5q-GUETw4WV1i": {
"-MYu5jNs2MMJJGB2L7jJ": {
"-MYu72gLxsi4M6J9aBAr": {
"title": "my first image",
"uri": "https://firebasestorage.googleapis.com/...",
"description": "some description",
"category": "some category"
}
}
}
}
}
Instead, save and use the reference returned by push():
DatabaseReference itemDataRef = referenceItems.child(mAuth.getCurrentUser().getUid()).push();
String itemId = itemDataRef.getKey();
/* ... */
StorageReference itemStorageRef = storageReference.child("Images").child(mAuth.getCurrentUser().getUid()).child(itemId).child("Pic1");
I am using data messages where we can send messages even when the app is killed or in background or in foreground. I am using FCM .
But in my case sometimes my app does not get those messages . I am sending messages from app to app. Sometimes the app get messages even when it is killed or removed from background but again , sometimes it wont.
When i open the app , then suddenly the message appears. I am opening activities when a particular message is received . I know that data messages are used for sending messages even when the app is killed or in background or in foreground , but i am having problem like this . please help !..
I want it to be absolute .
I just want my app to be connected to FirebaseMessagingServices always , even when it is killed. I don't know about services and some says that i need to create a foreground services . How to create it and implement to FirebaseMessagingServices .?
MYFirebaseMessaging.java
package com.example.praful.ubercoustomer.Service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import com.example.praful.ubercoustomer.AcceptedWindow;
import com.example.praful.ubercoustomer.Common.Common;
import com.example.praful.ubercoustomer.CompanycancelledtheBooking;
import com.example.praful.ubercoustomer.DeclinedWindow;
import com.example.praful.ubercoustomer.Helper.NotificationHelper;
import com.example.praful.ubercoustomer.Onthewayandimreached;
import com.example.praful.ubercoustomer.R;
import com.example.praful.ubercoustomer.RateActivity;
import com.example.praful.ubercoustomer.VerifyingCompletedBooking;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
public class MyFirebaseMessaging extends FirebaseMessagingService {
#Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
if (remoteMessage.getData() != null) {
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
final String companyName = data.get("CompanyName");
final String BookingIdC = data.get("BookingIdC");
final String BookingIdT = data.get("BookingIdT");
final String companyPhone = data.get("CompanyPhone");
final String companyRates = data.get("CompanyRates");
final String companyId = data.get("CompanyId");
final String Date = data.get("Date");
final String companyIdC = data.get("companyIdC");
final String Time = data.get("Time");
final String Id = data.get("Id");
final String Address = data.get("Address");
final String Bookingid = data.get("Bookingid");
final String TimeCB = data.get("TimeCB");
final String DateCB = data.get("DateCB");
final String EventType = data.get("EventType");
final String messageCB = data.get("messageCB");
final String AddressCB = data.get("AddressCB");
final String companythatcancelledthebooking = data.get("CompanyNamethatcancelledthebooking");
final String message = data.get("message");
// remoteMessage.getNotification().getTitle() = title and remoteMessage.getNotification().getBody() = message
if (title != null && title.equals("Cancel")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MyFirebaseMessaging.this, DeclinedWindow.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Common.isCompanyFound = false;
Common.companyId = "";
Toast.makeText(MyFirebaseMessaging.this, "" + message, Toast.LENGTH_SHORT).show();
}
});
} else if (title != null && title.equals("cancelAdvanceBooking")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getBaseContext(), CompanycancelledtheBooking.class);
intent.putExtra("DateCB", DateCB);
intent.putExtra("TimeCB", TimeCB);
intent.putExtra("messageCB", messageCB);
intent.putExtra("AddressCB", AddressCB);
intent.putExtra("EventType", EventType);
intent.putExtra("Id", Id);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Common.isCompanyFound = false;
Common.companyId = "";
Toast.makeText(MyFirebaseMessaging.this, "" + messageCB, Toast.LENGTH_SHORT).show();
}
});
} else if (title != null && title.equals("Accept")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MyFirebaseMessaging.this, AcceptedWindow.class);
intent.putExtra("Date", Date);
intent.putExtra("Time", Time);
intent.putExtra("Address", Address);
intent.putExtra("companyName", companyName);
intent.putExtra("companyPhone", companyPhone);
intent.putExtra("companyRates", companyRates);
intent.putExtra("companyId", companyId);
intent.putExtra("Bookingid", Bookingid);
intent.putExtra("EventType", EventType);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Common.isCompanyFound = false;
Common.companyId = "";
Toast.makeText(MyFirebaseMessaging.this, "" + message, Toast.LENGTH_SHORT).show();
}
});
} else if (title != null && title.equals("Arrived")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
showArrivedNotifAPI26(message);
else
showArrivedNotif(message);
}
});
} else if (title != null && title.equals("Completed")) {
openRateactivity(message);
} else if (title != null && title.equals("completedAdvancebooking")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MyFirebaseMessaging.this, VerifyingCompletedBooking.class);
intent.putExtra("BookingIdC", BookingIdC);
intent.putExtra("message", message);
intent.putExtra("companyid", companyIdC);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
} else if (title != null && title.equals("Ontheway")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MyFirebaseMessaging.this, Onthewayandimreached.class);
intent.putExtra("message", message);
intent.putExtra("BookingIdT", BookingIdT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
} else if (title != null && title.equals("Reached")) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MyFirebaseMessaging.this, Onthewayandimreached.class);
intent.putExtra("message", message);
intent.putExtra("BookingIdT", BookingIdT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
}
#RequiresApi(api = Build.VERSION_CODES.O)
private void showArrivedNotifAPI26(String body) {
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0,
new Intent(), PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationHelper notificationHelper = new NotificationHelper(getBaseContext());
Notification.Builder builder = notificationHelper.getUberNotification("Arrived", body, contentIntent, defaultSound);
notificationHelper.getManager().notify(1, builder.build());
}
private void openRateactivity(String body) {
Intent intent = new Intent(this, RateActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void showArrivedNotif(String body) {
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0,
new Intent(), PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getBaseContext());
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.setWhen(System.currentTimeMillis()).
setSmallIcon(R.drawable.ic_menu_camera)
.setContentTitle("Arrived")
.setContentText(body)
.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
}
MyFirebaseIdService
package com.example.praful.ubercoustomer.Service;
import com.example.praful.ubercoustomer.Common.Common;
import com.example.praful.ubercoustomer.Model.Token;
import com.example.praful.ubercoustomer.Common.Common;
import com.example.praful.ubercoustomer.Model.Token;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseIdService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
updateTokenToServer(refreshedToken);
}
private void updateTokenToServer(String refreshedToken) {
FirebaseDatabase db =FirebaseDatabase.getInstance();
DatabaseReference tokens = db.getReference(Common.token_table);
Token token = new Token(refreshedToken);
if(FirebaseAuth.getInstance().getCurrentUser() != null)
tokens.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(token);
}
}
First check this remote message contains payload, May be payload get corrupt for any reason.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "REMOTE_MSG";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage == null)
return;
// check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification body: " + remoteMessage.getNotification().getBody());
createNotification(remoteMessage.getNotification());
}
// check if message contains a data payload
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
}
DON'T forgent to update your FCM device token on your DB.
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {
private static final String TAG = "FCM_ID";
#Override
public void onTokenRefresh() {
// get hold of the registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
// lg the token
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// implement this method if you want to store the token on your server
}
}
Update 1
As mentioned Firebase repo issue expected that issue to be related to device, Please try another device aspect.
Update 2 Quote from firebase repo contributor kroikie
FCM does not process messages if an app is "killed" or force stopped.
When a user kills an app it is an indication that the user does not
want the app running so that app should not run till the user
explicitly starts it again.
Note that swiping an app from recents list should NOT "kill" or force
stop it. If messages are not being received after swiping from recents
list then please identify these devices and we will work with the
manufactures to correct this behaviour.
Only being able to handle messages when the app is in the foreground
or background is working as intended.
Update 3
Try hacks mentioned on this issue.
Inserting this lines in Manifest.xml
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
It is because of DOZE mode and battery optimization,you just have to turn off battery optimization for all apps or particular app. Go to Settings>> apps >> select your app>> battery>>
battery optimization> select your app>> select don't optimise.
Problem solved.
Now (for APP IS CLOSED case) I write Notification text to file and read this text if extras == null and notificationText.txt is exists... its stupid solution but it works. How can I catch this extras when app is closed in other way.
Update 4
Try to Setting the priority of a message,
Disable Battery Optimization
Users can manually configure the whitelist in Settings > Battery >
Battery Optimization. Alternatively, the system provides ways for apps
to ask users to whitelist them.
An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent
to take the user directly to the Battery Optimization, where they can
add the app. An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
permission can trigger a system dialog to let the user add the app to
the whitelist directly, without going to settings. The app fires a
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the
dialog. The user can manually remove apps from the whitelist as
needed.
Programmatically open Battery Optimization, check this answer.
TAKE CARE ABOUT THIS
Before asking the user to add your app to the whitelist, make sure the
app matches the acceptable use cases for whitelisting.
Note:
Google Play policies prohibit apps from requesting direct exemption from
Power Management features in Android 6.0+ (Doze and App Standby)
unless the core function of the app is adversely affected.
Update 5
Check PowerManager.isIgnoringBatteryOptimizations() from this answer.
When disable battery optimization, Consider to check first if it's already disabled then no need to show dialog, else you can show dialog.
/**
* return false if in settings "Not optimized" and true if "Optimizing battery use"
*/
private boolean checkBatteryOptimized() {
final PowerManager pwrm = (PowerManager) getSystemService(Context.POWER_SERVICE);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return !pwrm.isIgnoringBatteryOptimizations(getBaseContext().getPackageName());
}
}catch (Exception ignored){}
return false;
}
private void startBatteryOptimizeDialog(){
try {
Intent intent = new Intent(android.provider.Settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:PUT_YOUR_PACKAGE_NAME_HERE"));
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
I'm trying to code some unit test for some OnCreate() methode on my XMPP app on android Studio, the problem is that i have never done that and i'm a little bit lost.
Here is my methode :
public class ChatActivity extends AppCompatActivity {
private static final String TAG ="ChatActivity";
private String contactJid;
private ChatView mChatView;
private SendButton mSendButton;
private BroadcastReceiver mBroadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mChatView =(ChatView) findViewById(R.id.rooster_chat_view);
mChatView.setEventListener(new ChatViewEventListener() {
#Override
public void userIsTyping() {
//Here you know that the user is typing
}
#Override
public void userHasStoppedTyping() {
//Here you know that the user has stopped typing.
}
});
mSendButton = mChatView.getSendButton();
mSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Only send the message if the client is connected
//to the server.
if (RoosterConnectionService.getState().equals(RoosterConnection.ConnectionState.CONNECTED)) {
Log.d(TAG, "The client is connected to the server,Sending Message");
//Send the message to the server
Intent intent = new Intent(RoosterConnectionService.SEND_MESSAGE);
intent.putExtra(RoosterConnectionService.BUNDLE_MESSAGE_BODY,
mChatView.getTypedString());
intent.putExtra(RoosterConnectionService.BUNDLE_TO, contactJid);
sendBroadcast(intent);
//Update the chat view.
mChatView.sendMessage();
} else if (RoosterConnectionService.getState().equals(RoosterConnection.ConnectionState.DISCONNECTED)){
Toast.makeText(getApplicationContext(),
"Client not connected to server ,Message not sent!",
Toast.LENGTH_LONG).show();
}
}
});
Intent intent = getIntent();
contactJid = intent.getStringExtra("EXTRA_CONTACT_JID");
setTitle(contactJid);
}
My question is that i don't know how to proceed and what to put in my OnCreateTest() method and what to test, could anyone help please :(
I have built a WebView application in java.
Basically what i did was Created a WebView loaded a url, but what happened is when i click on the Uri tel:, the app crashes i have no idea what to do.
I have tried to override the Web. I did override WebViewClient.
App always crashing if uri = tel something by this code:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("html5rocks.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
Try this:
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}
You need ACTION_DIAL and not ACTION_VIEW.
i'm doing a project where i have to call a browser via my android app, but when i call it, the app stops.
the code can be found here: https://github.com/coppetti/android-pulsometer
but for fast view, i have a "Pulsometro" class where
public void onPreviewFrame(byte[] data, Camera cam) {
...
Browser browser = new Browser();
browser.callBrowser(beats);
return;
...
}
and a Browser class where:
public class Browser extends Activity{
public void callBrowser(int beats){
String url = "http://www.higia.info/?q="+beats;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return;
}
}
There's a way to call a browser and my app doesn't breaks?
Try this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onOpenWebBrowser(View v)
{
Intent webPageIntent = new Intent(Intent.ACTION_VIEW);
webPageIntent.setData(Uri.parse("https://www.google.co.in/"));
try {
startActivity(webPageIntent);
} catch (ActivityNotFoundException ex) {
}
}
Do not just randomly choose superclasses. Do not just create some subclass of Activity and expect it to work.
Move your callBrowser() method into some real Activity implementation, and get rid of Browser entirely.
Or, remove the superclass from Browser, have callBrowser() take a Context as a parameter, and call startActivity() on that Context.
Try this class. Call the callBrowser method and give it and Activity for the context parameter.
public class Browser{
public void callBrowser(Context context, int beats){
String url = "http://www.higia.info/?q="+beats;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i);
}
}