Android搶紅包助手開發(fā)全攻略
背景:新年之際,微信微博支付寶紅包是到處飛,但是,自己的手速總是比別人慢一點(diǎn)最后導(dǎo)致紅包沒搶到,紅包助手就應(yīng)運(yùn)而生。
需求:收到紅包的時候進(jìn)行提醒,然后跳轉(zhuǎn)到紅包的界面方便用戶。
思路:獲取“讀取通知信息”權(quán)限,然后開啟服務(wù)監(jiān)控系統(tǒng)通知,判斷如果是微信紅包就進(jìn)行提醒(聲音),然后跳轉(zhuǎn)到紅包所在的地方。
界面:

界面分為兩部分,一部分是可以對App進(jìn)行操作的,下面是一個可以滑動的界面,提示用戶如何是軟件正常工作,布局代碼如下:
<?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="打開提示音"/>
<CheckBox
android:id="@+id/isMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="音量調(diào)節(jié)"/>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="wrap_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="有紅包亮屏并解鎖"/>
<CheckBox
android:id="@+id/isUnlock"
android:layout_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="設(shè)置通知權(quán)限"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
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="聲明:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="本軟件為個人開發(fā)所得,只能對微信紅包進(jìn)行提醒。請合理使用本軟件,使用不當(dāng)造成的各種行為均與本人無關(guān)。軟件使用過程不聯(lián)網(wǎng),不存在任何盜竊用戶信息行為,請放心使用。"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="使用方法:"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="①如果未賦予軟件讀取通知權(quán)限,點(diǎn)擊按鈕“設(shè)置通知權(quán)限"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="②在本軟件右側(cè)勾選上,并確認(rèn)提示信息"/>
<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="③關(guān)閉微信群的消息免打擾(取消圖中的綠色按鈕)"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/inf2"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
app打開的時候開啟一個服務(wù),編寫一個NotificationListenerService的子類并實(shí)現(xiàn)onNotificationPosted和onNotificationRemoved方法,前面的方法會在收到通知的時候調(diào)用
// 編寫一個NotificationListenerService的子類并實(shí)現(xiàn)onNotificationPosted和onNotificationRemoved方法
// 這兩個方法在從SDK版本21的時候開始變成了非抽象,不重寫則不能兼容21以下設(shè)備
public class NotificationService extends NotificationListenerService {
private KeyguardManager.KeyguardLock kl;
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// 主界面設(shè)置的信息保存在SharedPreferences中,在這里進(jìn)行獲取
SharedPreferences sharedPreferences = getSharedPreferences("userdata", MODE_PRIVATE);
// 判斷消息是否為微信紅包
if (sbn.getNotification().tickerText.toString().contains("[微信紅包]") && sbn.getPackageName
().equals("com.tencent.mm")) {
// 讀取設(shè)置信息,判斷是否該點(diǎn)亮屏幕并解開鎖屏,解鎖的原理是把鎖屏關(guān)閉掉
if (sharedPreferences.getBoolean("isUnlock",true)) {
KeyguardManager km = (KeyguardManager) getSystemService(getApplicationContext()
.KEYGUARD_SERVICE);
kl = km.newKeyguardLock("unlock");
// 把系統(tǒng)鎖屏?xí)簳r關(guān)閉
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();
}
try {
// 打開notification所對應(yīng)的pendingintent
sbn.getNotification().contentIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
// 判斷是否該播放提示音
if (sharedPreferences.getBoolean("isMusic",true)){
MediaPlayer mediaPlayer = new MediaPlayer().create(this, R.raw.heihei);
mediaPlayer.start();
}
// 這里監(jiān)聽一下系統(tǒng)廣播,判斷如果屏幕熄滅就把系統(tǒng)鎖屏還原
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 intent) {
if (kl != null) {
// 還原鎖屏
kl.reenableKeyguard();
}
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
}
}
主的activity,注釋在代碼中了,就不詳細(xì)說了
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);
// 獲取控件實(shí)例
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);
// 注冊監(jiān)聽
isMusic.setOnCheckedChangeListener(this);
isUnlock.setOnCheckedChangeListener(this);
setPermision.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(this);
// 讀取設(shè)置信息
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);
// 獲得Audiomanager,控制系統(tǒng)音量
audioManager = (AudioManager) getSystemService(this.AUDIO_SERVICE);
seekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
// 監(jiān)聽系統(tǒng)媒體音量改變,并改變界面上的Seekbar的進(jìn)度
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
VolumReceiver receiver = new VolumReceiver();
registerReceiver(receiver,intentFilter);
// 開啟服務(wù)
Intent intent = new Intent(MainActivity.this, NotificationService.class);
startService(intent);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 判斷返回鍵點(diǎn)擊,提示用戶是否確認(rèn)退出
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Snackbar snackbar = Snackbar.make(root, "退出軟件", Snackbar.LENGTH_LONG)
.setAction("確認(rèn)", new View.OnClickListener() {
@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的點(diǎn)擊監(jiān)聽
switch (buttonView.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:
// 打開系統(tǒng)里面的服務(wù),方便用戶直接賦予權(quán)限
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) {
// seekbar的監(jiān)聽,滑動停止就修改系統(tǒng)媒體音量
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,seekBar.getProgress(),0);
}
// 音量廣播接收
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="com.fndroid.administrator.justforyou"
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"
android: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添加依賴,因?yàn)橛昧薙nackbar
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'
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 詳解Studio引用Library與導(dǎo)入jar
這篇文章主要介紹了Android Studio引用Library與導(dǎo)入jar的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android App中實(shí)現(xiàn)向右滑動銷毀功能的要點(diǎn)解析
這篇文章主要介紹了Android應(yīng)用中實(shí)現(xiàn)向右滑動銷毀條目功能的要點(diǎn)解析,有些類似于iOS App中的滑動頁面刪除效果,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)下載進(jìn)度條效果
vivo商店在下載應(yīng)用的時候,底部有一個圓角矩形的下載進(jìn)度條,中間有一個進(jìn)度文字,而且進(jìn)度和文字交匯的時候,交匯部分的文字會從藍(lán)色邊為白色,會有一種一半白色字,一半藍(lán)色字的效果。本文將仿照該樣式實(shí)現(xiàn)一個2021-06-06
Android 圖片添加水印的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 圖片添加水印的實(shí)現(xiàn)方法的相關(guān)資料,添加水印的原理就是在畫布Canvas上繪制圖形、圖片、文字等等, 得到你想要的效果圖片,需要的朋友可以參考下2017-07-07
Android RecycleView添加head配置封裝的實(shí)例
這篇文章主要介紹了Android RecycleView添加head配置封裝的實(shí)例的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08

