I have a service application. I need to send a string to any application (browser, word, ...wherever the keyboard cursor focused). How can I do this?
//onReceive of my service onStartCommand...
#Override
public void onReceive(Context context, Intent intent) {
String textToSend = intent.getStringExtra("data");
if(textToSend!=null && textToSend.length()>0) {
//Here I need send "textToSend" to another application
}else{
Toast.makeText(context, "Error! ", Toast.LENGTH_SHORT).show();
}
}
You can achieve it with Intents. Passing the string value in the URL, specifically: http://developer.android.com/training/sharing/send.html.
You can check this question for more info.
Related
I am trying to make a simple android app in which there is a toast message containing the number of the incoming call. I have stored the number in a string variable. When I display just the toast, it works fine but before the toast, if I add a simple if condition comparing the number to another string, the app quits. The required permissions are given. Can someone help me?
This Works:
public void onReceive(Context context, Intent intent) {
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
}
This Does not Work (App quits)
public void onReceive(Context context, Intent intent) {
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(incomingNumber.equals("+919999999999"))
{
Toast.makeText(context, "Call from Mom", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
}
}
It sounds a little silly but I just wrapped my "if" condition in a try-catch block and it solved the problem. I am not used to programming in JAVA so it took me a lot to figure out this simple thing. Thank You All for providing me with suggestions :)
I have something strange going on with my application.
I am trying to send a string via Broadcast by doing the following:
1st step (Sending):
Intent intent = new Intent("INFO");
intent.putExtra("INFO_VALUE", "hello_world_2019");
2nd step (Receiving):
if ("INFO".equals(intent.getAction())) {
String abc = intent.getStringExtra("INFO_VALUE");
Log.i(TAG, "" + abc);
}
Doing the previous steps, I get a null into my abc field. Also, if I use the debugger and check my intent related to the second step, I get:
intent -> mExtras -> mMap -> value[0] -> name: "hello_world_2019"
I am confused to what is going on. The abc field is not supposed to be null, but it is in this case.
How can I populate the aforementioned field so it is not null ?
Please explain what exactly you are trying to do, if you want to send data from one activity to other than my friend this is not the correct way to do that.
If you want to send a broadcast and receive that somewhere inside your code than you need to follow the below steps:
ReceiverActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("INFO"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
#Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
SenderActivity.java
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("INFO");
// You can also include some extra data.
intent.putExtra("message", "Message goes here!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
I'm building an application in which I use a service to create a background process. but I have a problem.
I use an EditText where the user enters a URL, then I will use that url in the service for check the connection to a website every X minutes. But if the user closes the application, the background process crash because the value of the EditText became Null, and logcat give me null pointer exceptions. I pass the value from the activity to the service with an intent, MainActivity code below:
public void onClickReadWebPage(View v)
{
if(address.getText().toString().trim().length() == 0)
{
Toast.makeText(getApplicationContext(), "URL String empty.", Toast.LENGTH_LONG).show();
}
else
{
time = String.valueOf(address.getText());
i=new Intent(MainActivity.this,MyService.class);
p=PendingIntent.getService(MainActivity.this, 0, i, 0);
i.putExtra("Address", time);
//start the service from here //MyService is your service class name
startService(i);
}
and this is the service code:
#Override
public void onStart(Intent intent, int startId)
{
Address = intent.getStringExtra("Address");
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(Address);
if(report == "false")
{
//do my stuff
}
else
{
//do my stuff
}
}
Any suggestion?
Just save the value of ediText in SharedPreferences and then retrieve them in your service
.See
How to use SharedPreferences in Android to store, fetch and edit values
Also Problems using SharedPreferences on a Service (getPreferences doesn't exist on a service)
I want to ask you:
1-How can I send SMS via my applicatin without moving the user to the Messages box
i just want to allow user to insert the text then he/she clicks on the send button
after that directly the text message should be sent to specific number that i write in the code?
I used this Code as shown below, it works well, but it moved me to the message box :(
any Suggestions ?
MyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String str= MyEditText.getText().toString();
// For SMS
Uri smsUri = Uri.parse("I wrote my number here");
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "Besm Allah");
intent.setType("vnd.android-dir/mms-sms"); startActivity(intent);
}
});
====================== After Updating ================================
You are using an Intent object (new Intent(Intent.ACTION_VIEW ... ) which invokes the built-in SMS application to help you send an SMS message. Therefore you are redirected to the message box.
For sending message process to be completed without being directed to the message box you need to use PendingIntent. Take a look at this great SMS tutorial which would quite possibly solve your problem. http://mobiforge.com/developing/story/sms-messaging-android
You have to use the provided intent. That allows the user to choose which app to use to send SMS. This is the way Android is designed. It gives the user choice about how apps can send SMS.
If you want to send SMS without the user using their chosen app, then you will need to use an SMS API from a commercial provider (e.g. esendex), and pay them to send SMS for you.
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, SMS.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, pi, null);
}
Take a close look at SmsManager to learn what all of the parameters will do for you.
Also here is the tutorial that this code snippet came from: http://mobiforge.com/developing/story/sms-messaging-android
I found the Perfect Sol. :
// Inside OnCreate()
Send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
HelperWordStr= HelperWords.getText().toString();
// For SMS
if (!b) {
try {
sendSMS("Receiver Number", HelperWordStr);
Toast.makeText(ChattingPage.this, "SMS Sent", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(ChattingPage.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
});
// Outside
public void sendSMS(String number, String msg) throws Exception {
if (!b) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg, null, null);
}
b = true;
}
public class Unistallreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if (action == null)
Log.e("uninstall ", "Action==null!");
else if ("android.intent.action.BOOT_COMPLETED".equals(action)) {
Log.e("uninstall ", "action :"+action);
}
}
}
Doing something like this, but can't get notification.Reference said :
public static final String ACTION_PACKAGE_REMOVED
Since: API Level 1
Broadcast Action: An existing application package has been removed from the device. The data contains the name of the package. The package that is being installed does not receive this Intent
A need this intent to set uninstall password.
Any suggestions??
Get rid of the "set uninstall password" requirement.