最近我每10秒使用一次线程显示祝酒服务
我添加了活动类
我想在android服务中。执行activity.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;
}
我使用PendingIntent ..但不行。
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();
}
};
}
请给我建议。我想在服务类中执行活动类。