I can not find answer for this problem.
I am building some Android application based on sms services and
I found callback very difficult.
I want/need to call my primary application class MainActivity
from inside function onReceive
(class SMSReceiver
).
Toast works perfectly but any attempt to call public void OnSmsReceived()
(class MainActivity
) is unsuccessful.
// Main class
public class MainActivity extends Activity {
public void OnSmsReceived() {
System.out.println(TAG + ": OnSmsReceived " + "OK ");
}
/* more not important at this moment code bellow */
}
// Brodcast class
public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "SMS received.", Toast.LENGTH_LONG).show();
System.out.println(TAG + ": " + "onReceive");
//How to call MainActivity form here?
}
}
Ok, I finaly made that working:
This is really working code
Thank you for intent
...
//Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sms.pack.sms201"
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.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="sms.pack.sms201.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>
<receiver
android:name="sms.pack.sms201.SMSReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
//SMSReceiver class
public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
public static String Test = "empty";
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "onReceive");
Toast.makeText(context, "SMS received.", Toast.LENGTH_LONG).show();
Bundle bundle = intent.getExtras();
if( bundle != null) {
Log.v(TAG, "onReceive.bundle != null");
Test = String.valueOf(bundle.size());
}
Intent callingIntent = new Intent(context, MainActivity.class);
callingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callingIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
String value = "Extra val=" + Test;
callingIntent.putExtra("SMSR", value);
context.startActivity(callingIntent);
}
}
//MainActivity class
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
TextView tvGenLog;
protected void appendGenLog(String txt) {
txt = tvGenLog.getText() + txt + "\r\n";
tvGenLog.setText(txt);
}
private String memberFieldString;
@Override
protected void onNewIntent(Intent intent) {
appendGenLog(TAG + ".onNewIntent");
memberFieldString = intent.getStringExtra("SMSR");
appendGenLog("memberFieldString=" + memberFieldString);
appendGenLog("Take data from SMSReceiver,");
appendGenLog("SMSR=" + SMSReceiver.Test);
super.onNewIntent(intent);
} // End of onNewIntent(Intent intent)
@Override
protected void onResume() {
super.onResume();
//appendGenLog(TAG + ".onResume");
if (getIntent()!=null && getIntent().getExtras()!=null) {
appendGenLog(TAG + ".onResume" + ":" + getIntent().getExtras().getString("SMSR"));
}
else {
appendGenLog(TAG + ".onResume" + ": null");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(TAG, ".onCreate");
tvGenLog = (TextView) findViewById(R.id.tvGenLog);
appendGenLog(TAG + " Created");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}