安卓手机后台Service自动转发短信

笔者是在学校用电信宽带的,有好基友向我要电信wifi Chinanet的密码用用,自己偷空写了一个安卓程序,用来自动判断接受到的短信;


MainActivity.java

package com.yinghualuo.getnewsms;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {
	private Intent intent;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intent = new Intent(MainActivity.this,PassSMSService.class);
		startService(intent);
	}
}

BootReceiver.java

package com.yinghualuo.getnewsms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {
	public void onReceive(Context arg0, Intent arg1) {
		Intent startServiceIntent = new Intent(arg0,PassSMSService.class);
		arg0.startService(startServiceIntent);
	}
}
PassSMSService.java

package com.yinghualuo.getnewsms;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class PassSMSService extends Service {
	public void onCreate() {
		new SmsRecevier();
		Log.i("Service", "onCreate");
		super.onCreate();
	}
	public IBinder onBind(Intent intent) {
		return null;
	}
}

SmsReceiver.java

package com.yinghualuo.getnewsms;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsRecevier extends BroadcastReceiver{
	//朋友号码
	private static final String mobile = "15555555555";
	private String receivedMobile = null;
	private String receivedContent = null;
	private String sendContent = null;
	public void onReceive(Context context, Intent intent) {
		// 获取新短信广播内容
		Bundle bundle=intent.getExtras();   
		if (bundle != null){
			Object[] messages=(Object[])bundle.get("pdus");
			SmsMessage[] smsMessages=new SmsMessage[messages.length];
			for (int i = 0; i < smsMessages.length; i++) {    
				  smsMessages[i]=SmsMessage.createFromPdu((byte[])messages[i]);    
			}
			receivedMobile = smsMessages[0].getOriginatingAddress();
			receivedContent = smsMessages[0].getMessageBody();
			// 显示短信内容
			Log.i("receivedMobile", receivedMobile);
			Log.i("receivedContent length", String.valueOf(receivedContent.length()));
			Log.i("receivedContent", receivedContent);
			
			if (isChinanetSMS(context, receivedContent, receivedMobile)) {
				//下面的转发给朋友
				sendContent = receivedContent.substring(27,39) + " 转发自:屌爆的HTC T328d";
				sendContent = sendContent.trim();
				sendSMS(context, sendContent, mobile);
				Log.i("sendmsmContent", sendContent);
				Log.i("sendmsmMobile", mobile);
				//提示发送短信
				Toast.makeText(context, "sendmsmTo "+mobile, Toast.LENGTH_LONG).show();
			}
		}
	}
	// 判断是否为Chinanet发来的密码短信
	private boolean isChinanetSMS(Context context, String content, String mobile){
		boolean result = false;
		if(mobile.equals("10001")){
			if(content.length() > 39){
				Log.i("SMS judge", content.substring(0, 15));
				if (content.substring(0, 15).equals(context.getString(R.string.sms10001))){
					result = true ;
				}
			}
		}
		return result;
	}
	// 发送短信
	private void sendSMS(Context context,String content,String mobile){
		SmsManager smsManager = SmsManager.getDefault();
		PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, new Intent(), 0);
		smsManager.sendTextMessage(mobile, null, content, sentIntent, null);
	}
}
string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">10001密码守护精灵</string>
    <string name="tips">这个程序自动转发来自10001的无线网密码</string>
	<string name="sms10001">尊敬的天翼用户,您的上网账号为</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yinghualuo.getnewsms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" android:maxSdkVersion="18"/>
    
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
	<action android:name="android.provider.Telephony.SMS_RECEIVED" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  
	
	
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="SmsRecevier" android:enabled="true">
	    	<intent-filter >    
				<action android:name="android.provider.Telephony.SMS_RECEIVED" />
			</intent-filter>    
  		</receiver>
  		<receiver android:name="BootReceiver" android:enabled="true">
		    <intent-filter>
			    <!-- 系统启动完成后会调用-->
			    <action android:name="android.intent.action.BOOT_COMPLETED" />
		    </intent-filter>
		</receiver>
        <activity
            android:name="com.yinghualuo.getnewsms.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.yinghualuo.getnewsms.PassSMSService" android:enabled="true" ></service>  
    </application>

</manifest>

这个只是我小试身手写的,分享自己的功能实现,如果程序严谨性和规范性有带提高的地方,欢迎向我吐槽

QQ:496761858

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android中,截屏需要访问系统级别的权限,因此在后台Service中进行截屏需要获取特殊的权限。以下是一些实现步骤: 1. 在AndroidManifest.xml文件中添加权限声明: ``` <uses-permission android:name="android.permission.READ_FRAME_BUFFER" /> ``` 2. 创建一个Service,并在onCreate()方法中获取WindowManager和DisplayMetrics对象: ``` private WindowManager mWindowManager; private DisplayMetrics mDisplayMetrics; @Override public void onCreate() { super.onCreate(); mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); mDisplayMetrics = new DisplayMetrics(); mWindowManager.getDefaultDisplay().getMetrics(mDisplayMetrics); } ``` 3. 在Service中创建一个Bitmap对象,并使用MediaProjectionManager类来获取MediaProjection对象: ``` private MediaProjectionManager mMediaProjectionManager; private MediaProjection mMediaProjection; private ImageReader mImageReader; private int mScreenWidth; private int mScreenHeight; @Override public int onStartCommand(Intent intent, int flags, int startId) { mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE); mMediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) intent.getParcelableExtra("data")); mScreenWidth = mDisplayMetrics.widthPixels; mScreenHeight = mDisplayMetrics.heightPixels; mImageReader = ImageReader.newInstance(mScreenWidth, mScreenHeight, PixelFormat.RGBA_8888, 1); mMediaProjection.createVirtualDisplay("ScreenCapture", mScreenWidth, mScreenHeight, mDisplayMetrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null); mHandler.postDelayed(new Runnable() { @Override public void run() { startCapture(); } }, 1000); return super.onStartCommand(intent, flags, startId); } private void startCapture() { Image image = mImageReader.acquireLatestImage(); if (image != null) { Bitmap bitmap = Bitmap.createBitmap(mScreenWidth, mScreenHeight, Bitmap.Config.ARGB_8888); bitmap.copyPixelsFromBuffer(image.getPlanes()[0].getBuffer()); image.close(); // 在这里进行截屏操作 } } ``` 4. 在startCapture()方法中进行截屏操作,并将截屏结果保存到文件或者发送到服务器等。 需要注意的是,上述代码只是截取屏幕的一帧,如果需要实现视频录制或者连续截屏,需要做相应的修改。此外,由于截屏需要访问系统级别的权限,因此需要确保用户已经同意了相关权限。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值