Android - Starting a service with extra information through put/getExtra - android

I've really tried to get through the intent.putExtra() and getIntent().getExtras() and apply them to one of the SimpleService tutorials. I know a lot of people have already asked "why is bundle extras always null?" I promise I tried to hack through the answers I found here for several hours before I considered posting but I don't think I'm advanced enough to really understand what it is I must be doing wrong with the minor snippets people are posting. As such I put in the full code of my activity and my service.
I think my issue is the that my starting intent (the one I create in my activity) doesn't exist in the context of my service. I wonder if maybe I'm using Intents in the wrong direction/purpose entirely? I did try an intent.putExtra in my service, to try to send a string the other direction, but those extras are always null, too. So at the risk of repetition, why is bundle extras always null? How do I make a single intent that exists both in the context of my activity and my service?
I should note that the code as displayed below obviously will have a null extras because I've commented out a few of my attempts to .getExtras() that have failed. I deleted the rest for the sake of cleanliness.
EDIT: The answer thanks to the replies, in code for the sake of those who have also been Googling for hours. Put this in your service (please note that the return START_REDELIVER_INTENT may be wrong):
#Override
public int onStartCommand( Intent intent , int flags , int startId )
{
super.onStartCommand(intent, flags , startId);
extras = intent.getExtras();
//just checking
if( extras != null )
Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show();
return START_REDELIVER_INTENT;
}
My activity (based on Sai Geetha's blog):
package com.example.BroadcastIntent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class BroadcastIntentActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.buttonStart);
start.setOnClickListener(startListener);
Button stop = (Button)findViewById(R.id.buttonStop);
stop.setOnClickListener(stopListener);
//the intent I'm using to start and stop the service -- the extras don't go anywhere....
intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class);
intent.putExtra("extratoservice", "if you can read this, it made it to the service" );
}
Boolean serviceRunning;
Intent intent;
//Clicks from Geetha's Blog
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(intent);
serviceRunning = true;
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
try
{
stopService(intent);
serviceRunning = false;
}
catch( Exception e)
{
Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show();
}
}
};
}
And this is my service:
package com.example.BroadcastIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class BroadcastService extends Service{
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
//extras = arg0.getExtras(); <-- this has null extras, too...
return null;
}
Bundle extras;
#Override
public void onCreate() {
super.onCreate();
// extras = getIntent().getExtras(); <-- this is undefined?
if( extras == null )
Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show();
}
}

you need to look at the onStartCommand() function (I'm not at a desktop so I can't conveniently link to the javadoc). This will receive the intent you passed and your extras should be available there.

I think you need to do this in your service..
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
to get the intent you're passing in... but I'm not 100% on this, so double check
http://developer.android.com/reference/android/app/Service.html

Related

Show toast soon as activity is visible

I'm trying to show a toast whenever the activity is visible to user but the message is not displaying at all.
I've the activity X which at certain point calls:
Intent t = new Intent(this, MainActivity.class);
t.putExtra( "toast", getString(R.string.subscribingInBackground));
t.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(t);
finish();
And at MainActivity besides lots of other stuff I've:
#Override
public void onResume() {
super.onResume();
if (this.adView != null) {
this.adView.resume();
}
String toast = getIntent().getStringExtra("toast");
if (toast != null)
Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
}
but toast is always null
what am i doing wrong?
Your FLAG_REORDER_TO_FRONT suggests that you believe that the activity already exists. If so, that activity will be called with onNewIntent(), and the Intent delivered to onNewIntent() will have your extra. getIntent() returns the Intent that was used to create the activity initially.
use this code
package com.example.abc.introscreen;
import android.content.Intent;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent t = new Intent(this,MainActivity.class);
t.putExtra( "toast", "Hello");
startActivity(t);
}
#Override
public void onResume() {
String toast = getIntent().getStringExtra("toast");
if (toast != null)
Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
super.onResume();
}
}
enter code here
You can override onNewIntent() to get the Intent which you used in ActivityX to start MainActivity.
Quoting from the documentation on Activity
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
So you can override onNewIntent() as follows:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
Then your Toast will be displayed.

Invoke service when screen turns on (Android)

I am creating a lockscreen app with facial recognition. As a first step I am creating a simple lockscreen that unlocks on Button click. The lockscreen should start when Switch on MainActivity is turned on. I created a Service which starts my lockscreen activity, but the activity does not show again once I turn off and then turn on my screen.
I am a beginner in android and don't understand what to do. I would be happy if i could get some help.
The java files are:
MainActivity.java
package com.sanket.droidlockersimple;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
public class MainActivity extends ActionBarActivity {
private Switch enableLocker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enableLocker = (Switch) findViewById(R.id.enableLocker);
enableLocker.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(enableLocker.isChecked()) {
Intent intent = new Intent(MainActivity.this, DroidLockerService.class);
startService(intent);
}
}
});
}
}
DroidLockerService.java
package com.sanket.droidlockersimple;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class DroidLockerService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Intent intent1 = new Intent(this, LockScreen.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
LockScreen.java
package com.sanket.droidlockersimple;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class LockScreen extends ActionBarActivity {
private Button unlock;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_lock_screen);
Button unlock = (Button) findViewById(R.id.button);
unlock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
I believe that what you need is a BroadcastReceiver
You have a service running, but something needs to tell the service that the screen has turned on or off. Android accomplishes this by issuing broadcast events whenever certain things occur (i.e. phone call received, system turned on, etc). Any registered broadcast receiver that has included the relevant BroadcastIntent will be informed that the event has occurred.
Your Broadcast Receiver can then tell your service that the event has occured and your service can respond appropriately.
I think that the particular broadcast events that you need to register for are "ACTION_SCREEN_ON" and "ACTION_SCREEN_OFF". See the Intent developer reference page for details.
Also read the developer guide for intents and intent filters in Android, and here is the BroadcastReceiver reference page. (StackOverflow won't let me post more than 2 links yet...just search for it on the Android developer website.)

Android - changing textview from other activity

I'm quite new to programming.
I'm trying to make a simple app with two activities, where the second activity can change the text of the first. I know it can be done using intents, but I was wondering if there is a more direct way of doing it, for example using the second activity to call a function from the first activity?
Here's the code I have so far:
The MainActivity, which contains a TextView and a button to open the second activity:
public class MainActivity extends Activity {
TextView textview;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.et2);
button = (Button) findViewById(R.id.b1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, ChangeText.class);
startActivity(intent);
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public void changetext(String message) {
textview.setText(message);
}
}
And the second activity, ChangeText, which contains an EditText and a button which should change the text of the TextView in MainActivity and then finish itself:
public class ChangeText extends Activity{
EditText edittext;
Button button;
private MainActivity mainclass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.change_text);
edittext = (EditText)findViewById(R.id.et2);
button = (Button)findViewById(R.id.b2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String message = edittext.getText().toString();
mainclass.changetext(message);
finish();
}
});
}
}
As you can see I tried to make the app work by making a public function in MainActivity which receives a string and sets the TextView with it, and then I call this function from the ChangeText activity.
The problem: it keeps crashing! Can anyone tell me how I can make this work?
Seems like I answered almost exactly the same question a week or so ago, so this is probably a duplicate, but I can't seem to find the original question.
The short answer is no - you can't call a method in an Activity from another Activity. The issue is that for normal programming purposes, only one Activity exists at a time*.
If you do something to circumvent this, then you're risking causing some major issues, including high memory usage, null pointer exceptions, etc.
The correct way to do this is indeed through the Intent system.
* Activities may or may not actually get destroyed when they become inactive, depending on things like how you use the back stack.
However, you should always program is if they do get destroyed when they become inactive - read, understand, and respect the Activity lifecycle.
For something as simple as your app, the "most direct" approach is to use the intent and startActivityForResult and them implement an onActivityResult in your main activity.
The problem you'll run into, even if you correctly pass you Activity references around is they are not guaranteed to be running at the same time.
Other ways aside from Intents, is to use a class(s) not involved with the Activity. Either a background service or a static variable in a class that extends Application. I rarely use Application classes anymore in favor of services and binding Activities them.
If I use an EventBus in projects, they can send Sticky events, which will hold the data until cleared.
Android uses a messaging mechanism to communicate between its components. This messaging mechnism is essential to Android, so you should use it. And as you already said, the messaging is implemented by Intents. ;-)
If you want something more complex, use an EventBus or implement a your own subscribe/publish mechanism that does what you want.
Use static variable
Example
In your MainActivity define
public static String msg = null;
then in your ChangeText activity assign changed text to it like
MainActivity.msg = edittext.getText().toString();
now in your main activity override the onResume() methode
if(msg != null){
textview.setText(msg);
msg = null;
}
You must LocalBroadCast Manager to do so
Here is the MainActivity which has the TextView which has to updated by another Activity
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)findViewById(R.id.txt1);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String s1= intent.getStringExtra("myString");
getIt(s1);
}
};
#Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
public void getIt(String s)
{
System.out.println(s);
txt.setText(s);
}
public void go(View view)
{
Intent intent = new Intent(this,WriteText.class);
startActivity(intent);
}
}
And This is the Activity which contains the EditText which updates the TextView in the previous activity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class WriteText extends Activity {
EditText ed1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_text);
ed1 = (EditText)findViewById(R.id.textView);
}
public void come(View view){
Intent intent = new Intent("custom-event-name");
intent.putExtra("myString", ed1.getText().toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
This Worked for me , Hope it works for you too

Android Messenger Application using services

I want to implement a simple messenger application for Android devices,I'm working with a web service which contains all the required methods for sending and receiving(by pressing the send button a record will be inserted in the DB and by calling the receive method all the rows related to this receiver(user) are retrieved).
I've written a service in a separate class and in onStart() I check the receive method of my .Net web service,I start the service in onCreate() of my activity ,so the service is in the background and receives the incoming messages perfectly,I can show the new message by using a toast directly in my service code,but I know that for accessing the views which are in my activity I should use pendingintent and maybe a BroadcastReceiver,so I can add the new messages to the main screen of my activity(for example a textview).
Now I want to find a way to access the textview of my activity and set the text of it through my service or anything else...
please help me on this issue,
Here is my activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyOwnActivity extends Activity
{
Button btnSend;
Button btnExtra;
EditText txtMessageBody;
TextView lblMessages;
BerryService BS = new BerryService();
public void SetMessageHistory(String value)
{
txtMessageBody.setText(value);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSend = (Button) findViewById(R.id.btnSend);
btnExtra = (Button) findViewById(R.id.btnExtraIntent);
txtMessageBody = (EditText) findViewById(R.id.txtMessageBody);
lblMessages = (TextView) findViewById(R.id.lblMessages);
/////////
//////////
startService(new Intent(this, IncomingMessageService.class));
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call webservice method to send
BS.SetSoapAction("http://tempuri.org/Send");
BS.SetMethodName("Send");
String a = BS.SendMessage(txtMessageBody.getText().toString());
lblMessages.setText(lblMessages.getText().toString() + "\n"
+ txtMessageBody.getText().toString());
txtMessageBody.setText("");
}
});
}
}
Here is my service:
import java.util.Timer;
import java.util.TimerTask;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.widget.Toast;
public class IncomingMessageService extends Service
{
private static final int NOTIFY_ME_ID = 12;
BerryService BS = new BerryService();
String text = "";
#Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Bind Failed");
}
#Override
public void onCreate() {
Toast.makeText(this, "onCreate", 5000).show();
}
#Override
public void onStart(Intent intent, int startId) {
// ////////////////////////
Toast.makeText(this, "onStart ", 1000).show();
// Timer Tick
final Handler handler = new Handler();
Timer _t = new Timer();
TimerTask tt = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "tick ", 1000)
.show();
// here the receive method should be called
BS.SetSoapAction("http://tempuri.org/RecieveMessage");
BS.SetMethodName("RecieveMessage");
String receivedMsg = BS.ReceiveMessage("sh");
//Instead of toast I want to access the textview in my activity!!!!!
Toast.makeText(getApplicationContext(), receivedMsg, 5000).show();
}
});
}
};
_t.scheduleAtFixedRate(tt, 0, 1000);
}
// /
#Override
public void onDestroy() {
Toast.makeText(this, "onDestroy", 5000).show();
}
}
You need to understand the concept of Broadcast, in your case it is the correct solution.
Start Broadcast in its activity
public static final String ACTION = "com.yourapp.ACTION.TEXT_RECEIVED";
private BroadcastReceiver mReceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
////////
mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
yourTextView.setText(msg);
}
};
IntentFilter filter = new IntentFilter(ACTION);
filter.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(mReceiver, filter);
////////
}
protected void onDestroy() {
// remember to unregister the receiver
super.onDestroy();
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
}
When you need to send the message of service you should use:
Intent i = new Intent();
i.setAction(MyOwnActivity.ACTION);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.putExtra("msg", "the message received by webservice");
i.putExtras(b);
sendBroadcast(i);
Have a look here: http://developer.android.com/reference/android/content/BroadcastReceiver.html
Using a broadcast manager is great but I personally prefer to use square's Otto because it is just so easy to perform communication between components in an android application.
http://square.github.io/otto/
If you do choose to use otto, you are going to have to override the Bus's post method to be able to talk post messages to a bus on the foreground. Here is the code for that:
public class MainThreadBus extends Bus {
private final Handler handler = new Handler(Looper.getMainLooper());
#Override public void post(final Object event) {
if (Looper.myLooper() == Looper.getMainLooper()) {
super.post(event);
} else {
handler.post(new Runnable() {
#Override
public void run() {
post(event);
}
});
}
}
}

How to pass a value in Services?

I am making a media player ,who can run in background. i have to send a string uri in this service.
but i cant send the uri value from my activity through bundle.Services class doesnot support getIntent() and show error.
how can i send uri in service through activity.
please help
package com.abc.activity;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
String uri,url;
Bundle b;
AudioPlayer2 ap;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Log.d(TAG, "onCreate");
Uri path=Uri.parse("/mnt/sdcard/download/abc.mp3");
player = MediaPlayer.create(this,path );
player.setLooping(false); // Set looping
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
//Bundle extras = getIntent().getExtras();
player.start();
}
public void getValue(String uri)
{
url=uri;
}
}
This is working with static url which i use in this example. please suggest what can i fetch uri's value from my activity class.Bundle is not working in this class
You should use IntentService http://developer.android.com/reference/android/app/IntentService.html.
Implement onHandleIntent(Intent intent) which will be called each time you start service with the startService(intent). You can pass your data to service through this intent.
If you are trying to send a string or String-array from one Activity to another this can be done in the Intent.
In ClassA:
Intent intent = new Intent(this, ClassB);
tring[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
In ClassB:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}

Categories

Resources