android 系统静音广播,Android开发那些事(超级短信 时候默认把系统音量调到最大,然后音频提醒)...

问题产生:

情景一:

有一次和一朋友半夜短信聊天,忽然可能是短信的延时,过了很久才收到她发来的短信,而且那晚我也很困,结果,结果。。。就这么睡着了。。。早上醒来挺无奈的。

情景二:

由于平时要上课,我的手机默认都是静音 ,是连振动都没打开的那种,下课后也没调回去, 一次陈老师发短信过来让我马上修改一些个人资料,手机没提示,差点耽搁了事情。

问题解决:

纠结情景一的问题一段时间之后,忽然想到,如果能写一个程序, 能捕获短信并提取到对方的号码,就能设置收到特定人物发来的短信时,手机有相应的提醒操作。例如情景一中,打开这个程序,收到短信后,手机会无限次有节奏 狂震,那么估计我能醒过来了。

思路:

1.在Activity中定义广播内部类,用来捕捉短信广播。

2.Activity中有一个编辑框,一个确认按钮,用来测试或添加临时超级短信号码,并使用SharedPreferences储存,保证不会重启程序后号       码丢失。另一个按钮用来停止振动。

3.使用开机自启动和后台自启,不必每次开机手动打开程序。

4.收到超级短信后,手机无限次有节奏振动,点亮屏幕并自动解锁。

查了一些资料,终于写了这个程序。

代码:

SMSActivity.java

package com.example.mytext;

import android.app.Activity;

import android.app.KeyguardManager;

import android.app.KeyguardManager.KeyguardLock;

import android.app.Service;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.media.AudioManager;

import android.os.Bundle;

import android.os.PowerManager;

import android.os.Vibrator;

import android.telephony.SmsManager;

import android.telephony.SmsMessage;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class SMSActivity extends Activity {

/** Called when the activity is first created. */

EditText myedittext;

Button QR;

Button stop;

String str;

Vibrator vb;//定义振动器

AudioManager am;//音频管理器

PowerManager pm;//电源管理器

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.mainx);

QR = (Button) findViewById(R.id.myButtontoQR);

stop = (Button) findViewById(R.id.myButtontostop);

vb = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);

myedittext = (EditText) findViewById(R.id.myEditText);

//内部广播类

class myBroadcast extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

final String action = "android.provider.Telephony.SMS_RECEIVED";

//开机自启动

if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {

Intent intent1 = new Intent();

intent1.setClass(context, SMSActivity.class);

intent1.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(intent1);

}

if (intent.getAction().equals(action)) {

SmsManager sms = SmsManager.getDefault();

Bundle bundle = intent.getExtras();

String to = null;

String msg = null;

if (bundle != null) {

Object[] pdus = (Object[]) bundle.get("pdus");

SmsMessage[] messages = new SmsMessage[pdus.length];

for (int i = 0; i < pdus.length; i++)

messages= SmsMessage

.createFromPdu((byte[]) pdus);

for (SmsMessage message : messages) {

msg = message.getMessageBody();

to = message.getOriginatingAddress();

}

// Toast.makeText(context, to,

// Toast.LENGTH_LONG).show();

SharedPreferences sp = context.getSharedPreferences(

"my_sp", context.MODE_PRIVATE);

String Number = sp.getString("Number", "XXXXXX");                        //XXXXXX是手机号码// Toast.makeText(context, "超级号码是:"+Number+"发送方的号码是"+to,

// Toast.LENGTH_LONG).show();

if (Number.length() == 11)

Number = "+86" + Number;

if (to.equals(Number) || to.equals("XXXXXX")                                //XXXXXX是手机号码|| to.equals("+86XXXXXXXXXXX")

|| to.equals("+86XXXXXXXXXXX")

|| to.equals("XXXXXX") || to.equals("XXXXXX")) {

vb.vibrate(new long[] { 1000, 3000 }, 0);

pm = (PowerManager) context

.getSystemService(Service.POWER_SERVICE);

PowerManager.WakeLock mwakelock = pm.newWakeLock(

PowerManager.ACQUIRE_CAUSES_WAKEUP

| PowerManager.FULL_WAKE_LOCK,

"WakeLock");

mwakelock.acquire();

KeyguardManager mManager = (KeyguardManager) context

.getSystemService(Service.KEYGUARD_SERVICE);

KeyguardLock mKeyguardLock = mManager

.newKeyguardLock("Lock");

// 让键盘锁失效

mKeyguardLock.disableKeyguard();

}

}

}

}

}

myBroadcast mybroad = new myBroadcast();

//订阅短信广播

IntentFilter filter = new IntentFilter();

filter.addAction("android.provider.Telephony.SMS_RECEIVED");

registerReceiver(mybroad, filter);

//订阅开机广播

IntentFilter filter2 = new IntentFilter();

filter2.addAction("android.intent.action.BOOT_COMPLETED");

registerReceiver(mybroad, filter2);

QR.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

str = myedittext.getText().toString();

//记录测试的号码

SharedPreferences sp = getSharedPreferences("my_sp",

MODE_PRIVATE);

Editor editor = sp.edit();

editor.putString("Number", str);

editor.commit();

myedittext.setText("");

Toast.makeText(SMSActivity.this, "成功记录号码:" + str,

Toast.LENGTH_LONG).show();

}

});

stop.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

// String state = "QX";

// Intent intent = new

// Intent("android.intent.action.MY_BROADCAST");

// intent.putExtra("state", state);

// sendBroadcast(intent);

vb.cancel();

}

});

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

// unregisterReceiver(mysmsr);

}

}

布局:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/myEditText"

android:layout_width="300px"

android:layout_height="wrap_content"

android:inputType="phone"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/myButtontoQR"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="确认"

/>

android:id = "@+id/myButtontostop"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="停止"

/>

AndroidManifest.xml  相应权限:

截图:

431527a780b04b2c2684fb5c81f25714.png  

9c1184ff38a265b0185beaa648880855.png

后记:

本来想加入音频控制,当收到超级短信 时候默认把系统音量调到最大,然后音频提醒,考虑到上课和其他一些情况,先不加上去了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值