我正在制作一个即使关闭应用程序也可以发送通知的Android应用程序。所以我添加了一个NotificationService在应用程序销毁时调用。但是我收到一个错误:当我强制停止应用程序时,onCreate()之前的Activities无法使用系统服务。
这是MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private static final String CHANNEL_ID = "telus";
private static final String CHANNEL_NAME = "telus app";
private static final String CHANNEL_DESC = "telus app notification";
private int count = 1;
Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity_web();
}
});
button2=(Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivationPage();
}
});
button3=(Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openMysql();
}
});
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(CHANNEL_DESC);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayNotification();
}
});
class sendnotif extends TimerTask {
public void run() {
displayNotification();
}
}
Timer timer = new Timer();
timer.schedule(new sendnotif(), 0, 5000);
}
public void displayNotification(){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle("Titre de la notification")
.setContentText("C'est le text de la notification")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
;
NotificationManagerCompat mNotifMgr = NotificationManagerCompat.from(this);
mNotifMgr.notify( 1, mBuilder.build());
}
public void openActivity_web(){
Intent intent = new Intent(this, WebPage.class);
startActivity(intent);
}
public void openActivationPage(){
Intent intent = new Intent(this, ActivationPage.class);
startActivity(intent);
}
public void openMysql(){
Intent intent = new Intent(this, mysql_display.class);
startActivity(intent);
}
protected void onDestroy(){
super.onDestroy();
startService(new Intent(this, NotificationService.class));
}
}
这是NotificationService.java:
public class NotificationService extends Service {
Timer timer;
TimerTask timerTask;
String TAG = "Timers";
int Your_X_SECS = 5;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
stoptimertask();
super.onDestroy();
}
//we are going to use a handler to be able to run in our TimerTask
final Handler handler = new Handler();
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
timer.schedule(timerTask, 5000, Your_X_SECS * 1000); //
//timer.schedule(timerTask, 5000,1000); //
}
public void stoptimertask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void initializeTimerTask() {
timerTask = new TimerTask() {
public void run() {
//use a handler to run a toast that shows the current timestamp
handler.post(new Runnable() {
public void run() {
MainActivity notif = new MainActivity();
notif.displayNotification();
}
});
}
};
}
}
这是我尝试手动停止应用程序时的错误日志:
2019 - 07 - 16 20: 10: 23.079 23334 - 23334 / com.example.testapp E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testapp, PID: 23334
java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java: 5581)
at androidx.core.app.NotificationManagerCompat. < init > (NotificationManagerCompat.java: 158)
at androidx.core.app.NotificationManagerCompat.from(NotificationManagerCompat.java: 153)
at com.example.testapp.MainActivity.displayNotification(MainActivity.java: 103)
at com.example.testapp.NotificationService$1$1.run(NotificationService.java: 89)
at android.os.Handler.handleCallback(Handler.java: 751)
at android.os.Handler.dispatchMessage(Handler.java: 95)
at android.os.Looper.loop(Looper.java: 154)
at android.app.ActivityThread.main(ActivityThread.java: 6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 756)
谢谢您的帮助。