我尝试了很多方法。
我没有说,但它是针对 targetSdkVersion="8"
的,你给我的一些功能在这个旧系统上不起作用,
我找不到使用上下文和意图的工作方式。这总是导致崩溃。
我在 MainActivity 中找到了使用静态字段和函数的方法,
即使它可以工作并打印一些东西,之后它也会崩溃......
毕竟这对我来说非常有效。
当然它是草稿,但这允许与主类一起工作。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sms.pack.sms0"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.BROADCAST_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="sms.pack.sms0.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>
</application>
</manifest>
字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">sms0</string>
<string name="action_settings">Settings</string>
<string name="sGenInfo">GenInfo</string>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/sms.pack.sms0"
android:id="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvGenInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sGenInfo" />
</LinearLayout>
MainActivity.java
package sms.pack.sms0;
import android.util.Log;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
TextView tvGenInfo;
public void appendGenInfo(String txt) {
Log.v(TAG, "genLog, txt=" + txt);
txt = tvGenInfo.getText() + txt + "\r\n";
tvGenInfo.setText(txt);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(TAG, "onCreate");
tvGenInfo = (TextView) findViewById(R.id.tvGenInfo);
IntentFilter filter = new IntentFilter(Intent.ACTION_DEFAULT);
filter.addAction(SMS_RECEIVED);
filter.setPriority(1000);
this.registerReceiver(this.smsReceiver, filter);
appendGenInfo("Created");
}
private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "smsReceiver.onReceive, " + "");
Toast.makeText(context, "smsReceiver.onReceive, " + "", Toast.LENGTH_SHORT).show();
System.out.println(TAG + ", smsReceiver.onReceive, " + "");
Bundle bundle = intent.getExtras();
if( bundle != null) {
System.out.println(TAG + ", smsReceiver.onReceive.bundle, size=" + bundle.size());
appendGenInfo(TAG + ", smsReceiver.onReceive.bundle, " + "bundle.size=" + bundle.size());//this works
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}