Hi fellow coders I really need help and I am really out of ideas, tried a lot of ideas and none worked for me....
I am busy with a emission survey application and I am trying to change the tab within the if statement, if the value being broadcasted is 1, it go to the second tab and then to the third automatically... Attached is snippets, please help, I would really be grateful....!
The first image is of me sending the data back to the first tab
(All is still working at this moment)
loadB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTab(0);
sendData3();
}
private void sendData3() {
take = "1";
Intent dataIntent = new Intent();
dataIntent.setAction("com.example.e3soft.receiver");
dataIntent.putExtra("taken", take);
sendBroadcast(dataIntent);
Toast.makeText(getBaseContext(), "Working", Toast.LENGTH_LONG).show();
}
});
Without the if statement everything works, please help........!
private void receiveData() {
// TODO Auto-generated method stub
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
take = intent.getStringExtra("taken");
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.e3soft.receiver");
registerReceiver(receiver, filter);
changeTab();
}
protected void changeTab() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), take, Toast.LENGTH_LONG).show();
try {
if (take.equals("1")) {
populateVariables();
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTab(1);
sendData();
}
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG)
.show();
}
}
move your this code inside onReceive :
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.e3soft.receiver");
registerReceiver(receiver, filter);
changeTab();
Related
please before you don't like my question , please read the details .. what i am trying to do is to use a broadcast receiver when screen-off , so i want my app to start if the screen goes off .. here is my broadcast receiver code :
public class BootReceiver extends BroadcastReceiver {
public boolean screenoff;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenoff = true;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenoff = false;
}
Intent intent1 = new Intent(context, ShakeService.class);
intent1.putExtra("screen_state", screenoff);
context.startService(intent1);
}
and here is the service code :
public class ShakeService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stu
return null;
}
#Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new BootReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
try{
Intent intent1 = new Intent(getApplicationContext(), SplashScreen.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return START_STICKY;
}
}
and here is my splash screen activity that i call from the service :
public class SplashScreen extends Activity {
private static int SPLASH_TIME_OUT=3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
new Handler().postDelayed(new Runnable(){
public void run(){
Intent i =new Intent(SplashScreen.this,HomeScreen.class);
startActivity(i);
finish();
}
},SPLASH_TIME_OUT);
}
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
BroadcastReceiver mReceiver = new BootReceiver();
registerReceiver(mReceiver, filter);
super.onResume();
}
#Override
protected void onPause() {
// mSensorManager.unregisterListener(mShakeDetector);
BroadcastReceiver mReceiver = new BootReceiver();
unregisterReceiver(mReceiver);
super.onPause();
}
}
and as you can noticed i have unregistered my receiver but still i keep seeing this in the logcat :
leaked Intent Receiver are you missing a call to unregisterReceiver??
I agree with the method of registering the receiver in onResume() and unregistering it in onPause(). This helps when the app goes in and out of scope. I'm somewhat confused on what you're trying to do with your app, but initially I see that you create a new instance of the braodcast receiver in onPause() and unregister that. Try unregistering using the same instance . Also, if you want your service to remain running in the background, you may want to look into implementation of a partial wake lock, that keeps the CPU running for your app while the screen remains off. Could you provide one more details about the purpose of the app?
How to use pending intent to Toast something after a specified time when an app closes? Is there any other ways to do it?
I tried Broadcast receiver but don't know how to proceed.
public class Broad extends BroadcastReceiver {
#Override
public void onReceive(Context c, Intent i) {
// TODO Auto-generated method stub
i= new Intent(Intent.CATEGORY_HOME);
PendingIntent pi = PendingIntent.getBroadcast(c, 0, i, 0);
Toast.makeText(c, getResultData(), Toast.LENGTH_SHORT).show();
}
}
What to do next? Please help. I don't understand the tutorials as I'm new to android.
try this method:
public static void toastInTenSeconds(final Context context, final String text) {
new Thread(new Runnable() {
#Override
public void run() {
SystemClock.sleep(1000 * 10);
try {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// Do something
}
}
}
}
Call it from your Actvitiy onDestroy or onStop.
i'm trying to wait for the wifi scan to finish in the broadcast receiver!.
I have 1 Activty (ChooseActivity extends Activity) and 1 class (Scan).
In my Activity i call Scan class to scan wifi and return boolean (true) when finish, here is the code.
My purpose is to separate the scan of my activity because i call scan class in several places.
public class ChooseActivity extends Activity implements OnClickListener{
private int idMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose);
idMap = this.getIntent().getExtras().getInt("ID_MAP");
((Button)this.findViewById(R.id.scan_button)).setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.scan_button:
Scan scan = new Scan();
Toast.makeText(getApplicationContext(), " "+scan.scanHotspots(idMap), Toast.LENGTH_SHORT).show();
break;
}
}
}
And here is my Scan class
public boolean scanHotspots(int idMap){
wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
receiver = new Receiver();
IntentFilter intentFilter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
context.registerReceiver(receiver, intentFilter);
//HERE I WANT STOP EXECUTION TO WAIT test VAIRABLE CHANGE STATUS
return test;
}
public class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
{
test=true;
}
}
}
}
Thank you in advance.
I guess I have more like a design issue than a "how to bind a service" issue. What I'm trying to do is to have a service running in a separate process that handles bluetooth communication. Further I have several tabs, within each another activity. The processing and UI events from each activity results in simple commands which have to be passed over bluetooth like "up", "down", "left" or "right". As the results in each tab activity are the same, I don't wnat to connect each with the service, and use the same message handler. So I created my own Handler object and a "connector" object. However I'm not sure how to connect this connector object with my service as it requries a context. Does it make sense to simply pass the application context to my connector object and bind it that way???
I'm open for any suggestions or hints
I'd suggest against using binding in this case. Actually, I'd suggest against binding in most use cases. Try to handle all communication between you Activities and the service running in another thread using intents only.
That means:
Send commands to the service from the activities by the .startActivity() method, passing the details of the actual command in the Intent's extras.
Receive events and result from the service in your activities by dynamically registered BroadcastReceivers. You register a listener in onResume() and unregister it in onPause() in you Activity. The service sends stuff only by broadcasting it (sendBroadcast()).
I prefer this architecture. It's loosely coupled and you can skip the annoying part where you're wondering if every one of your Activities unbinds correctly from you service when they're not using it. Also, you skip the pain of using IPC, which is a huge plus I think.
Here you are, my example .. will make you clear about that LOL
// My MyServiceInterface.aidl
package com.mad.exam;
interface MyServiceInterface{
int getNumber();
}
//MyService
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service OnBind()", Toast.LENGTH_LONG).show();
return mBinder;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Service Destroyed ", Toast.LENGTH_SHORT).show();
}
#Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "Service Started ", Toast.LENGTH_SHORT).show();
}
#Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
private final MyServiceInterface.Stub mBinder = new MyServiceInterface.Stub() {
public int getNumber() {
return new Random().nextInt(100);
}
};
}
//My Activity
public class ServiceDemo extends Activity implements OnClickListener {
MyServiceInterface mService;
ServiceConnection mConnection;
Button retreive;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.service);
retreive = (Button) findViewById(R.id.retreive);
retreive.setOnClickListener(this);
mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mService = MyServiceInterface.Stub.asInterface(service);
try {
int i;
i = mService.getNumber();
Toast.makeText(ServiceDemo.this, "The service value is: " + String.valueOf(i),
Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("My Tag", "Clicked");
Button btn = (Button) v;
Intent callService = new Intent(this, MyService.class);
bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Intent callService = new Intent(this, MyService.class);
bindService(callService, mConnection, Context.BIND_AUTO_CREATE);
}
}
I am working on the application,I am having the requirement:if user long press the power button at any time and press again to start device,the activity should start from where the device was shut down.I dont know either this is a valid question or not.
I tried working with:
public class PowerMangerTestActivity extends Activity {
private static PowerManager objpowermanager;
private static PowerManager.WakeLock wl;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callTOWakeLock();
setContentView(R.layout.main);
}//end of onCreate
public void callTOWakeLock() {
// TODO Auto-generated method stub
objpowermanager=(PowerManager)getSystemService(Context.POWER_SERVICE);
wl=objpowermanager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "******MyTag****");
//You should acquire it when your app starts,
if(wl.isHeld())
{
wl.release();
}
wl.acquire();
}
#Override
public void onDestroy() {
wl.release();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
callTOWakeLock();
}
/*#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Intent i = new Intent(this, PowerMangerTestActivity.class);
startActivity(i);
return true;
TextView tv=(TextView) findViewById(R.id.textview);
tv.setText("You press power button");
}
return super.dispatchKeyEvent(event);
}*/
}
but not getting expected behaviour.
I have tried to catch KeyEvent.KEYCODE_POWER,but not getting how to use for this scenario.
any suggestions?
thanks
This may be unacceptable to you, but you should NOT DO THIS.
It is going against a usability idea called "expected behavior".
You are doing something that a user is not expecting, and there is a good chance they are going to be pissed if you do this.
Just my two cents!
I would try and find another solution to this problem, such as caching useful data in phone storage, and recalling it when the application is resumed/started.
onPause still should be called when the power button is long pressed. The only case (as far as I know) where it won't be called is on a battery pull
It is reasonable for an emergency manager app to catch the power button event, so here's one solution, taken from Samsungs support database:
package com.samsung.lockscreenreceiver;
public class LockScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(Intent.ACTION_USER_PRESENT.equals(action)) {
// bring app to foreground
} else if(Intent.ACTION_SCREEN_OFF.equals(action) ) {
Toast.makeText(context, "screen on",Toast.LENGTH_LONG).show();
} else if (Intent.ACTION_SCREEN_ON.equals(action)) {
Toast.makeText(context, "screen off",Toast.LENGTH_LONG).show();
}
}
}
And the activity:
public class MyActivity extends Activity {
private BroadcastReceiver mReceiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReceiver = new LockScreenReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
intentFilter.addAction(Intent.ACTION_USER_PRESENT);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
intentFilter.addAction(Intent.ACTION_SCREEN_ON);
//intentFilter.addAction(Intent.ACTION_SHUTDOWN); // won't work unless you're the device vendor
registerReceiver(mReceiver, intentFilter);
}
#Override
protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(mReceiver);
}
}
You catch power on/off and bring the activity to foreground. The activity then denies all focus changes using:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
//super.onWindowFocusChanged(hasFocus);
if(!hasFocus) {
Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(closeDialog);
}
}
And you'll have to switch off screen lock, for example using:
https://play.google.com/store/apps/details?id=org.jraf.android.nolock&hl=en
The above app is not protected, so you can decompile it as usual. :)