0

recently I using thread every 10 second showing toast service

and I add activity class

I want in android service. execute activity.class and showing activity screen

I try in service class

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notifi_M =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
thread = new ServiceThread(handler);
thread.start();     ---- existing code

Intent i = new Intent(this, SubActivity.class);    
PendingIntent p = PendingIntent.getActivity(this, 0, i, 0);
try {
    p.send();
} catch (PendingIntent.CanceledException e) {
    e.printStackTrace();
}
return START_STICKY;
}

I use PendingIntent .. but not work.

MainActivity.class

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(MainActivity.this,MyService.class);
    startService(intent);
}
}

ServiceThread.class

public class ServiceThread extends Thread{
Handler handler;
boolean isRun = true;

public ServiceThread(Handler handler){
    this.handler = handler;
}

public void stopForever(){
    synchronized (this) {
        this.isRun = false;
    }
}

public void run(){
    while(isRun){
        handler.sendEmptyMessage(0);           
        try{
            Thread.sleep(10000); 
        }catch (Exception e) {}
    }
}
}

MyService.class

public class MyService extends Service {
NotificationManager Notifi_M;
ServiceThread thread;
Notification Notifi ;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Notifi_M = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    myServiceHandler handler = new myServiceHandler();
    thread = new ServiceThread(handler);
    thread.start();

    Intent i = new Intent(this, CCTVActivity.class);
    PendingIntent p = PendingIntent.getActivity(this, 0,i,0);
    try {
        p.send();
    } catch (PendingIntent.CanceledException e) {
        e.printStackTrace();
    }
    return START_STICKY;
}



public void onDestroy() {
    thread.stopForever();
    thread = null;
}

class myServiceHandler extends Handler {
    @Override
    public void handleMessage(android.os.Message msg) {
        Intent intent = new Intent(MyService.this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MyService.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

        Notifi = new Notification.Builder(getApplicationContext())
                .setContentTitle("Content Title")
                .setContentText("Content Text")
                .setSmallIcon(R.drawable.logo)
                .setTicker("call!!!")
                .setContentIntent(pendingIntent)
                .build();


        Notifi.defaults = Notification.DEFAULT_SOUND;


        Notifi.flags = Notification.FLAG_ONLY_ALERT_ONCE;


        Notifi.flags = Notification.FLAG_AUTO_CANCEL;


        Notifi_M.notify( 777 , Notifi);


        Toast.makeText(MyService.this, "???", Toast.LENGTH_LONG).show();
    }
};
}

please advice for me. I want in service class , execute activity class.

chohyunwook
  • 361
  • 1
  • 2
  • 15

0 Answers0