Now many applications have connected to the push function, and there are also many third parties about push on the market, such as Aurora, etc., so we don’t have much demand. Accessing Aurora will cause a lot of waste of resources. Let’s take a look at using android services to push messages locally.
1. Register a Service
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by 70883 on 2017/8/10. */ public class PushSmsService extends Service { private NotificationManager manager; private PendingIntent pi; private MyThread myThread; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { myThread = new MyThread(); (); (); } @Override public void onDestroy() { (); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void notification() { // Get the notification manager of the system manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(getApplicationContext(), ); pi = (getApplicationContext(), 0, intent, 0); Notification notification = new (getApplicationContext()) .setAutoCancel(true) .setContentText("When you are busy with work, you have to have a meal.") .setContentIntent(pi) .setSmallIcon(.ic_icon) .setWhen(()) .build(); = Notification.DEFAULT_ALL; // Use default settings such as ringtones, vibrations, flashes = Notification.FLAG_AUTO_CANCEL; // But after the user clicks on the message, the message will automatically disappear in the notification bar |= Notification.FLAG_NO_CLEAR;// Click the notification bar to delete the message, and the message will not be deleted. (0, notification); } private class MyThread extends Thread{ private Calendar c ; @Override public void run() { while (true){ c = (); if((Calendar.HOUR_OF_DAY) == 15){ try { notification(); sleep(1000*60*60); } catch (InterruptedException e) { (); } } } } } }
2. Register in AndroidMan
<service android:name="."></service>
3. Since I need the global application, I started it in Application.
public void startService() { Intent intent = new Intent(this, ); // Start the service startService(intent); }
4. It can also be used with the server to push messages regularly
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.