background: on the occasion of the New Year, micro-credit micro-bo pay Bao is flying everywhere, but, their hands are always slower than others eventually lead to red envelopes did not Rob, the red envelope assistants came into being.
Requirements: When you receive a red envelope, remind, and then jump to the interface of red envelopes user-friendly.
train of thought: get "read notification information" permission, and then open the Service Monitoring System notification, judge if it is micro-letter red envelopes to remind (voice), and then jump to the place where the red envelope.
Interface:
The interface is divided into two parts, some of which can operate on the app, the following is a sliding interface, prompting the user how the software works correctly, the layout code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout android:id= "@+id/root" xmlns:android= "http://" Schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_
Parent "android:layout_height=" match_parent "android:layout_marginleft=" 10DP "android:layout_marginright=" 10DP " android:layout_margintop= "5DP" android:orientation= "vertical" tools:context= "
Com.fndroid.administrator.justforyou.MainActivity "> <linearlayout android:layout_width=" match_parent " android:layout_height= "wrap_content" android:orientation= "horizontal" > <textview android:layout_width= "Wrap" _content "android:layout_height=" Wrap_content "android:layout_weight=" 3 "android:text=" open beep "/> <CheckBo X android:id= "@+id/ismusic" android:layout_width= "wrap_content" android:layout_height= "wrap_content"/> </ linearlayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "WR"Ap_content "> <textview android:layout_width=" wrap_content android:layout_height= "Wrap_content" Android: layout_gravity= "Center" android:text= "volume adjustment"/> <seekbar android:id= "@+id/seekbar" W
Rap_content "android:layout_height=" wrap_content "android:layout_gravity=" center "android:layout_weight=" 1 "/>
</LinearLayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content"
android:orientation= "Horizontal" > <textview android:layout_width= "0dp" android:layout_height= "Wrap_content" android:layout_weight= "3" android:text= "have red envelopes and unlock"/> <checkbox android:id= "@+id/isunlock" Android:lay Out_width= "Wrap_content" android:layout_height= "wrap_content"/> </LinearLayout> <button android:id= "@+ Id/setpermision "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_gravity=" "Center" android:text= settingNotification Permissions "/> <scrollview android:layout_width=" match_parent android:layout_height= "match_parent" > <Linear Layout android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical"
; <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_marginTop = "10DP" android:text= declaration: "/> <textview android:layout_width=" wrap_content "android:layout_height=" WR Ap_content "android:text=" the software for the personal development of income, can only be reminded of the micro-letter red envelopes. Please use the software reasonably, all kinds of behavior caused by improper use have nothing to do with me. Software use process is not networked, there is no theft of user information behavior, please feel free to use. "/> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layo" ut_margintop= "10DP" android:text= "use method:"/> <textview android:layout_width= "Wrap_content" Android:lay out_height= "Wrap_content" android:text= ① if the software is not given permission to read notifications, click the button "Set notification Permissions"/> <textview "android:layout_width= wr" Ap_content "Android:layout_height= "Wrap_content" android:text= "② on the right side of the software, and confirm the message"/> <imageview android:layout_width= "wrap_content "Android:layout_height=" wrap_content "android:scaletype=" Fitcenter "android:src=" @drawable/inf "/> < TextView android:layout_width= "wrap_content" android:layout_height= wrap_content "android:text=" ③ to turn off the message of the micro-group to avoid interruption (take Eliminate the green button in the graph) "/> <imageview android:layout_width=" wrap_content "android:layout_height=" Wrap_content "and
roid:src= "@drawable/inf2"/> </LinearLayout> </ScrollView> </LinearLayout>
Open a service When app opens, write a notificationlistenerservice subclass and implement onnotificationposted and Onnotificationremoved methods, The previous method invokes when the notification is received;
Writing a Notificationlistenerservice subclass and implementing the Onnotificationposted and Onnotificationremoved methods//The two methods began to become non-abstract from the SDK version 21 , no Override is not compatible with 21 of the following devices public class Notificationservice extends Notificationlistenerservice {private
Keyguardmanager.keyguardlock KL; @Override public void onnotificationposted (Statusbarnotification SBN) {//main interface settings are stored in sharedpreferences, where they are obtained Shar
Edpreferences sharedpreferences = getsharedpreferences ("UserData", mode_private); Determine if the message is a micro-letter red envelope if (Sbn.getnotification (). Tickertext.tostring (). Contains ("[Micro-Red Envelopes]") && sbn.getpackagename (). E Quals ("com.tencent.mm")) {//Read setup information to determine if the screen is lit and unlock the screen, the principle of unlocking is to turn off the lock screen if (Sharedpreferences.getboolean ("Isunlock", True) {Keyguardmanager km = (keyguardmanager) getsystemservice (Getapplicationcontext ().
Keyguard_service);
KL = Km.newkeyguardlock ("unlock");
The System lock screen is temporarily closed Kl.disablekeyguard (); PowerManager pm = (powermanager) getsystemservice (Getapplicationcontext ().
Power_service); PoWermanager.wakelock wl = Pm.newwakelock (Powermanager.acquire_causes_wakeup |
Powermanager.screen_dim_wake_lock, "bright");
Wl.acquire ();
Wl.release ();
The try {//Open notification corresponds to the pendingintent sbn.getnotification (). Contentintent.send ();
catch (Pendingintent.canceledexception e) {e.printstacktrace (); //Determine if the play beep if (Sharedpreferences.getboolean ("Ismusic", True)) {MediaPlayer MediaPlayer = new MediaPlayer ().
Create (this, R.raw.heihei);
Mediaplayer.start ();
//Here listen to the system broadcast, judge if the screen is extinguished to the system Lock screen restore Intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction ("Android.intent.action.SCREEN_OFF");
Screenoffreceiver screenoffreceiver = new Screenoffreceiver ();
Registerreceiver (Screenoffreceiver, Intentfilter); Class Screenoffreceiver extends Broadcastreceiver {@Override public void onreceive (context context, Intent in
Tent) {if (KL!= null) {//Restore lock screen kl.reenablekeyguard ();
}
}
} @Override public void onnotificationremoved (Statusbarnotification sbn) {super.onnotificationremoved (SBN);
}
}
The activity of the master, annotated in the code, does not elaborate on the
public class Mainactivity extends Appcompatactivity implements Compoundbutton.
Oncheckedchangelistener, View.onclicklistener,seekbar.onseekbarchangelistener {private LinearLayout root;
Private CheckBox Ismusic;
Private CheckBox Isunlock;
Private Sharedpreferences.editor Editor;
Private Sharedpreferences sharedpreferences;
Private Button setpermision;
Private SeekBar SeekBar;
Private Audiomanager Audiomanager;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Gets the control instance root = (LinearLayout) Findviewbyid (r.id.root);
Ismusic = (CheckBox) Findviewbyid (r.id.ismusic);
Isunlock = (CheckBox) Findviewbyid (R.id.isunlock);
Setpermision = (Button) Findviewbyid (r.id.setpermision);
SeekBar = (SeekBar) Findviewbyid (R.id.seekbar);
Register to monitor Ismusic.setoncheckedchangelistener (this);
Isunlock.setoncheckedchangelistener (this);
Setpermision.setonclicklistener (this); seekbar.seTonseekbarchangelistener (this);
Read settings Information sharedpreferences = getsharedpreferences ("UserData", mode_private);
Editor = Sharedpreferences.edit ();
Boolean music = Sharedpreferences.getboolean ("Ismusic", true);
Boolean unlock = Sharedpreferences.getboolean ("Isunlock", true);
Ismusic.setchecked (music);
isunlock.setchecked (unlock); Get Audiomanager, control system Volume Audiomanager = (Audiomanager) Getsystemservice (this.
Audio_service);
Seekbar.setmax (Audiomanager.getstreammaxvolume (audiomanager.stream_music));
Seekbar.setprogress (Audiomanager.getstreamvolume (audiomanager.stream_music));
Monitor system media volume changes and change the Seekbar progress on the interface intentfilter intentfilter = new Intentfilter ();
Intentfilter.addaction ("Android.media.VOLUME_CHANGED_ACTION");
Volumreceiver receiver = new Volumreceiver ();
Registerreceiver (Receiver,intentfilter);
Open service Intent Intent = new Intent (mainactivity.this, Notificationservice.class);
StartService (Intent); @Override public boolean onKeyDown (int keyCode, KeyEvent event) {//Judge return key Click, Prompt user whether to confirm exit if (keycode = = Keyevent.keycode_back && event.getrepeatcount () = = 0 {Snackbar Snackbar = Snackbar.make (Root, "Exit software", Snackbar.length_long). Setaction ("Confirm", new view.onclicklist
Ener () {@Override public void OnClick (View v) {MainActivity.this.finish ();
}
});
Snackbar.show ();
return true;
Return Super.onkeydown (KeyCode, event); @Override public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {//checkbox click on Monitor switch (b
Uttonview.getid ()) {case R.id.ismusic:editor.putboolean ("Ismusic", ischecked);
Editor.commit ();
Break
Case R.id.isunlock:editor.putboolean ("Isunlock", ischecked);
Editor.commit ();
Break @Override public void OnClick (View v) {switch (V.getid ()) {case r.id.setpermision://Open the service within the system to facilitate direct user assignment
Grant permission Intent Intent = new Intent (settings.action_notification_listener_settings); StartActivity (intent);
Break @Override public void onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser) {} @Override Public void Onstarttrackingtouch (SeekBar SeekBar) {} @Override public void Onstoptrackingtouch (SeekBar SeekBar) {//Seek
Bar monitoring, sliding stop to modify the system media Volume Audiomanager.setstreamvolume (audiomanager.stream_music,seekbar.getprogress (), 0); //volume broadcast receive class Volumreceiver extends broadcastreceiver{@Override public void onreceive (context context, Intent
Intent) {seekbar.setprogress (Audiomanager.getstreamvolume (audiomanager.stream_music));
}
}
}
mainfest
<?xml version= "1.0" encoding= "Utf-8"?> <manifest "package=" xmlns: Android= "Http://schemas.android.com/apk/res/android" > <uses-permission android:name= " Android.permission.BIND_NOTIFICATION_LISTENER_SERVICE "/> <uses-permission android:name="
Android.permission.WAKE_LOCK "/> <uses-permission android:name=" Android.permission.DISABLE_KEYGUARD "/>" <application android:allowbackup= "true" android:icon= "@mipmap/ic_launcher" android:label= "@string/app_name" and Roid:supportsrtl= "true" android:theme= "@style/apptheme" > <activity android:name=. Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category Android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android: Name= ". Notificationservice "android:permission=" Android.permission.BIND_NOTIFICATION_LISTENER_SERVICE "> <intent-filter> <action android:name= "Android.service.notification.NotificationListenerService"/> </intent
-filter> </service> </application> </manifest>
Gradle add dependencies because of the use of Snackbar
dependencies {
Compile filetree (dir: ' Libs ', include: [' *.jar '])
testcompile ' junit:junit:4.12 ' compile '
com.android.support:appcompat-v7:23.1.1 '
compile ' com.android.support:design:23.1.1 '
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.