-4

I am working on an android application where an action is triggered after specific interval of time again and again when there is an Intent.FLAG_ACTIVITY_NEW_TASK Broadcast message received. I have the following code in UpdateService.java:

package com.missnoob.screentimeout;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class UpdateService extends Service {

    @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 ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Log.v("ScreenTimeOut","Broadcast Received");
            Toast.makeText(getApplicationContext(), "Broadcast Received", Toast.LENGTH_LONG).show();

            //This part is not working
            Intent i = new Intent(this, Notification.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            //This part is not working

        } 
        else 
        {
            // YOUR CODE
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

And the following code in Notification.java:

package com.missnoob.screentimeout;

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.shadow.screentimeout.R;

public class Notification extends Activity {
/** Called when the activity is first created. */

Timer timer;
Toast toast;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timer = new Timer();
    toast = Toast.makeText(getApplicationContext(), "15 seconds after",Toast.LENGTH_SHORT);

    timer.scheduleAtFixedRate(new TimerTask() 
        {
            @Override
            public void run() {
                toast.show();
                Log.v("ScreenTimeOut","Toast showed");

            }
        }, 0, 5000);

    }

}

In UpdateService.java

Intent i = new Intent(this, Notification.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

never gets triggered.

Miss Noob
  • 169
  • 3
  • 12

3 Answers3

1

You are missing startActivity(i)

 Intent i = new Intent(this, Notification.class);
 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(i);

Also you cannot have

 toast.show(); 

in timer as it runs on a different thread and you can update ui only on the ui thread.

Instead use a Handler. You can find an example @

Android Thread for a timer

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

You have not called startActivity(i).

See Start Another Activity (android developer website).

shkschneider
  • 17,833
  • 13
  • 59
  • 112
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
1

You are didn't call startActivity(i), thats why your activity is never caled, all you have to do is like this :

 Intent i = new Intent(this, Notification.class);
 i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(i);
Menma
  • 799
  • 1
  • 9
  • 35