要在安卓手机上通过代码查询未读短信,你需要访问手机的SMS Content Provider。这是实现的一种方式:
public void checkUnreadSms() {
final String[] projection = new String[]{"_id", "address", "person", "body", "date", "type", "read"};
boolean flag = false;
Cursor cursor = null;
try {
cursor = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/inbox"), projection, "read=?", new String[]{"0"}, null);
int indexBody = cursor.getColumnIndex("body");
if (null != cursor && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
String strbody = cursor.getString(indexBody);
Log.i("Info", "body: " + strbody);
} while (cursor.moveToNext());
if (!cursor.isClosed()) {
cursor.close();
cursor = null;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
cursor = null;
}
}
}
请注意调用上述代码获取短信需要有READ_SMS权限,请在你的AndroidManifest.xml中添加以下权限:
<uses-permission android:name="android.permission.READ_SMS" />
另外,从Android 6.0开始,检查和请求权限更加严格,你需要运行时请求权限:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, MY_PERMISSIONS_REQUEST_READ_SMS);
}
请注意,这是一种在安卓设备上直接读取未读短信的方法,和阿里云或者南宁或者代理商没有直接的关系。
以下是安卓未读短信的代码实例,这需要你的应用具有读取短信的权限。
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = null;
try {
ContentResolver contentResolver = getContentResolver();
cursor = contentResolver.query(uriSms, new String[]{BaseColumns._ID, "address", "date", "body", "read"}, "read=?", new String[]{"0"}, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String id = cursor.getString(cursor.getColumnIndex(BaseColumns._ID));
String address = cursor.getString(cursor.getColumnIndex("address"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String read = cursor.getString(cursor.getColumnIndex("read"));
String body = cursor.getString(cursor.getColumnIndex("body"));
Log.d(TAG, "id: " + id + ", address: " + address + ", date: " + date + ", read: " + read + ", body: " + body);
} while (cursor.moveToNext());
}
}
} catch (Exception e) {
Log.d(TAG, "Error while trying to get unread messages: ", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
关于权限声明,在你的AndroidManifest.xml文件中添加以下权限:
<uses-permission android:name="android.permission.READ_SMS" />
这段代码将会查询所有的未读短信,并在LogCat中打印出每条短信的ID、发件人、日期、是否已读和内容。如果你需要做更复杂的处理,你可以在对应的位置添加你的代码。
注意,因为这样的功能涉及到用户隐私,需要用户授予应用读取短信内容的权限,你必须妥善保护用户的隐私。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/167183.html