找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8901|回复: 1
打印 上一主题 下一主题
收起左侧

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

[复制链接]
跳转到指定楼层
楼主
ID:51088 发表于 2014-8-21 03:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
问题产生:

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

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


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

思路:
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);
}

}


布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    >

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="300px"
        android:layout_height="wrap_content"
        android:inputType="phone"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
        android:id="@+id/myButtontoQR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确认"
        />

        <Button
            android:id = "@+id/myButtontostop"
            android:layout_width="wrap_content"
           android:layout_height="wrap_content"
            android:text="停止"
            />

    </LinearLayout>



</LinearLayout>


AndroidManifest.xml  相应权限:

    <uses-permission android:/>
    <uses-permission android:/>
    <uses-permission android:/>  
   <uses-permission android:/>
   
    <uses-permission android: />   
    <uses-permission android: />   
    <uses-permission android: />  

    <uses-permission android:name ="android.permission.WAKE_LOCK"/>
    <uses-permission android:/>

    <uses-permission android:/>


截图:

  



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


  




评分

参与人数 1威望 +5 黑币 +5 收起 理由
absflash + 5 + 5 眼前一亮,

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:84495 发表于 2015-7-5 09:08 | 只看该作者
本帖最后由 absflash 于 2015-7-5 09:10 编辑

看到朋友帖子很高兴,想请教一问题,不知朋友是否接触过蓝牙串口调试助手(手机的),,我想串口助手收到蓝牙模块发来的某一代码后   触发手机震动或声音,越剧烈越好。。请高人指教!!!本人只懂一点单片机c语言,也是业余自学的,没接触过安卓开发
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表