I know there are several answers to this question, but it isn't working for me, currently my code is:
public class LogoutService extends Service {
public static CountDownTimer timer;
#Override
public void onCreate(){
// TODO Auto-generated method stub
super.onCreate();
timer = new CountDownTimer(1 * 60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
Log.v(TAG, "Service Started");
}
public void onFinish() {
Log.v(TAG, "Call Logout by Service");
// Code for Logout
stopSelf();
}
};
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
and in every activity I have:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
LogoutService.timer.start();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
LogoutService.timer.cancel();
}
But I get a "Unable to resume activity java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.CountDownTimer android.os.CountDownTimer.start()' on a null object reference" when I try to start any of my activities that contain this code. My application isn't too complicated, it's completely offline, so no web service calls or anything, I just need to go back to my login activity after x minutes. Anyone have any ideas?
Here's the one I used: Auto logout after X minutes, Android and http://androidjug.blogspot.com/2015/10/auto-logout-after-15-minutes-due-to.html
You aren't starting the service, so you aren't allocating the timer. Really you should not be using a static for this.
Related
I am developing an android application and I want close the window of view adds after I click on the adds window using Resume event.I put a rule in resume event but this rule make adds never show. so if there is a way to make the view adds disappears when I return to my application?
this is my code:
#Override
protected void onResume() {
// TODO Auto-generated method stub
if (adView != null) {
adView.destroy();
}
super.onResume();
}
this is the right code:
protected void onResume() {
// TODO Auto-generated method stub
adView.setAdListener(new AdListener() {
#Override
public void onAdOpened() {
// Save app state before going to the ad overlay.
}
#Override
public void onAdClosed() {
adView.setVisibility(View.GONE);
// TODO Auto-generated method stub
super.onAdClosed();
}
});
super.onResume();
}
use following to disappear the view.
adView.setVisibility(View.GONE):
set an onClickListener on adView then in onClick call this method.
How can I write command in onclicklistener() so that I can move to next activity as well as my post status dialog will appear on that new activity. I am using intent for switching to next activity and also using postTowall() method. But these two doesn't perform simultaneously. I am using this method:
public void onClick(View v) {
// TODO Auto-generated method stub
postTowall();
Intent intent= new Intent(Frnd.this,Logout.class);
startActivity(intent);
}
private void postTowall() {
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
public void onComplete(Bundle values) {
String sh = null;
Bundle params = new Bundle();
params.putString("caption", sh);
}
public void onCancel() {
// TODO Auto-generated method stub
}
You can use thread for posting. Use asynctask and inside that post the message to face book. Or you may just write a simple thread and start it.
This asynctask link may help you. it has usage example also:
http://developer.android.com/reference/android/os/AsyncTask.html
You should set other activity to open post dialog. Send via intent a signal if it should or not...
I got a problem which I can't understand. I'm trying to solve it for 8 days and still stuck. I asked about that more experienced developers and they can't answer. So please, I desperately ask for help
the service is simple - it has one method yet - it should give me Log - getLog()
public class AudioService extends Service{
MyBinder binder = new MyBinder();
public void getLog(){Log.d("MyLog","I reached getLog!");}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}
class MyBinder extends Binder {
AudioService getService() {
return AudioService.this;
}
}
}
MainAcivity crashes when i try to reach audioService.getLog(); It doesn't crash,however, if i insert line AudioService audioService= new AudioService(); but that's what I don't want - I need to set a Service which will play audio so I can start mp3 from one activity and stop it from another. here's MainActivity :
public class MainActivity extends Activity {
ServiceConnection sConn;
Intent intservice;
AudioService audioService ;
boolean bound=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intservice=new Intent(this,AudioService.class);
sConn=new ServiceConnection(){
#Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
// TODO Auto-generated method stub
audioService = ((AudioService.MyBinder) binder).getService();
bound=true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
bound=false;
}
};
startService(intservice);
audioService.getLog();
}
#Override
protected void onStart() {
super.onStart();
bindService(intservice, sConn, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
if (!bound) return;
unbindService(sConn);
bound = false;
}
}
in case I messed something in Manifest I shall include part with service here :
</activity>
<service android:enabled="true" android:name="AudioService"></service>
</application>
</manifest>
I can't understand what's wrong here so please give me advice where I did wrong
Well, I've found why it was. Binding is a procedure that needs time and every example existing waits for onClick which means Activity has enough time between onStart() and onCreate(). In my case it goes immediately. So the best way is to move all Activity under public void onServiceConnected(ComponentName arg0, IBinder binder). Which should mean that after binding happened - Activity goes on well.
Hope that will help to many people )
I wrote this app that in the first screen it has an included Thread on it. So it has I timed it like 7 seconds then it will proceed to the next activity.
The problem is whenever I hit the home button the music will stop and it will go to android homescreen but after my timed is done which is the 7 seconds, the app will reappear and will show the next activity.
I tried putting finish(); in the onpause(); but it's still showing the next activity.
here's the actual code.
public class HelloWorldActivity extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mp = MediaPlayer.create(this, R.raw.otj);
mp.start();
Thread LogoTimer = new Thread(){
public void run(){
try{
int LogoTimer = 0;
while(LogoTimer < 7000){
sleep(100);
LogoTimer = LogoTimer + 100;
}
startActivity(new Intent("com.example.HelloWorld.CLEARSCREEN"));
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
LogoTimer.start();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
First, that's a really inefficient way to run a timer. Try this way instead:
new Handler().postDelayed(new Runnable() {
public void run() {
// Do some work.
}
}, delayTimeInMs);
Second, your starting a new activity when that timer eventually fires. It doesn't matter that the originating activity is finished. Your startActivity() is running on it's own thread and will execute regardless.
It's possible the postDelayed() method will function like you expect. If not you'll need to have it check when it runs whether it should really start the activity. However, I think the Handler is attached to the default Looper which means it will stop (or rather, the message won't be posted) if the main activity finishes.
The application is still in the background and the thread is not destroyed so it will fire the startActivity.
I would not really setup a splash screen this way, or use a thread unless I wanted it off the UI for some reason, even then there are better options.
For educational purposes to take care of this you need to be able to abort the thread safely in onPause() one way to do so is below
Modifed Thread
Thread LogoTimer = new Thread() {
private volatile boolean abortThread = false;
public void run(){
long stopAt = System.currentTimeMillis() + 7000;
while (!abortThread && stopAt > System.currentTimeMillis())
yield();
if (!abortThread)
startActivity ...
}
public synchronized void stopThread() {
abortThread = true;
}
};
I have designed for schedule SMS in android.i set timer for SMS to send by using CountDownTimer Constructor class. values for timer assigned,but i cannot start the countdown time even i have passed the timer value and interval values. If i start the constructor manually by using object, the sms is sent immediately(not waiting for the time set). I need to send the sms when the time set to send is reached .Here goes my code. Any Help is Appreciated.
new Mycounter(dif, 1000);
// My Counter timer code goes here
public class Mycounter extends CountDownTimer
{
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
public Mycounter(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
#Override
public void onFinish() {
sendSMS(phoneNo, message);
// TODO Auto-generated method stub
}
#Override
public void onTick(long millisUntilFinished)
{
// TODO Auto-generated method stub
}
}
Are you calling start()?
new Mycounter(dif, 1000).start()
Otherwise your timer code looks good.
Add few logs.