国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android搶紅包助手開發全攻略

2019-12-12 06:02:42
字體:
來源:轉載
供稿:網友

背景:新年之際,微信微博支付寶紅包是到處飛,但是,自己的手速總是比別人慢一點最后導致紅包沒搶到,紅包助手就應運而生。
需求:收到紅包的時候進行提醒,然后跳轉到紅包的界面方便用戶。
思路:獲取“讀取通知信息”權限,然后開啟服務監控系統通知,判斷如果是微信紅包就進行提醒(聲音),然后跳轉到紅包所在的地方。 
界面:

界面分為兩部分,一部分是可以對App進行操作的,下面是一個可以滑動的界面,提示用戶如何是軟件正常工作,布局代碼如下:

<?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="音量調節"/>  <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="設置通知權限"/> <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="本軟件為個人開發所得,只能對微信紅包進行提醒。請合理使用本軟件,使用不當造成的各種行為均與本人無關。軟件使用過程不聯網,不存在任何盜竊用戶信息行為,請放心使用。"/>   <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="①如果未賦予軟件讀取通知權限,點擊按鈕“設置通知權限"/>   <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="②在本軟件右側勾選上,并確認提示信息"/>   <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="③關閉微信群的消息免打擾(取消圖中的綠色按鈕)"/>   <ImageView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/inf2"/>  </LinearLayout> </ScrollView></LinearLayout> 

app打開的時候開啟一個服務,編寫一個NotificationListenerService的子類并實現onNotificationPosted和onNotificationRemoved方法,前面的方法會在收到通知的時候調用 

// 編寫一個NotificationListenerService的子類并實現onNotificationPosted和onNotificationRemoved方法// 這兩個方法在從SDK版本21的時候開始變成了非抽象,不重寫則不能兼容21以下設備public class NotificationService extends NotificationListenerService { private KeyguardManager.KeyguardLock kl; @Override public void onNotificationPosted(StatusBarNotification sbn) {  // 主界面設置的信息保存在SharedPreferences中,在這里進行獲取  SharedPreferences sharedPreferences = getSharedPreferences("userdata", MODE_PRIVATE);  // 判斷消息是否為微信紅包  if (sbn.getNotification().tickerText.toString().contains("[微信紅包]") && sbn.getPackageName    ().equals("com.tencent.mm")) {   // 讀取設置信息,判斷是否該點亮屏幕并解開鎖屏,解鎖的原理是把鎖屏關閉掉   if (sharedPreferences.getBoolean("isUnlock",true)) {    KeyguardManager km = (KeyguardManager) getSystemService(getApplicationContext()      .KEYGUARD_SERVICE);    kl = km.newKeyguardLock("unlock");    // 把系統鎖屏暫時關閉    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所對應的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();   }   // 這里監聽一下系統廣播,判斷如果屏幕熄滅就把系統鎖屏還原   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,注釋在代碼中了,就不詳細說了 

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);  // 獲取控件實例  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);  // 注冊監聽  isMusic.setOnCheckedChangeListener(this);  isUnlock.setOnCheckedChangeListener(this);  setPermision.setOnClickListener(this);  seekBar.setOnSeekBarChangeListener(this);  // 讀取設置信息  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,控制系統音量  audioManager = (AudioManager) getSystemService(this.AUDIO_SERVICE);  seekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));  seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));  // 監聽系統媒體音量改變,并改變界面上的Seekbar的進度  IntentFilter intentFilter = new IntentFilter();  intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");  VolumReceiver receiver = new VolumReceiver();  registerReceiver(receiver,intentFilter);  // 開啟服務  Intent intent = new Intent(MainActivity.this, NotificationService.class);  startService(intent); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) {  // 判斷返回鍵點擊,提示用戶是否確認退出  if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {   Snackbar snackbar = Snackbar.make(root, "退出軟件", Snackbar.LENGTH_LONG)     .setAction("確認", 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的點擊監聽  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:    // 打開系統里面的服務,方便用戶直接賦予權限    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的監聽,滑動停止就修改系統媒體音量  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添加依賴,因為用了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'}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 随州市| 木里| 云浮市| 广元市| 金山区| 湄潭县| 秭归县| 垦利县| 潞西市| 台州市| 微山县| 西林县| 华坪县| 安西县| 汉中市| 宾川县| 新干县| 富裕县| 宁蒗| 蓬溪县| 韶山市| 肥城市| 堆龙德庆县| 萝北县| 灵寿县| 无棣县| 无为县| 玉龙| 三门县| 津市市| 玛沁县| 兰西县| 武鸣县| 习水县| 肥东县| 瓦房店市| 利辛县| 朝阳县| 长丰县| 西青区| 邢台县|