I want to do the same as indicated in the question in the following link. Call the action method from the BroadcastReceiver class . But the solution given there does not work for me, and I can not comment there because I do not have enough reputation. I did everything that is mentioned in the solution, but my broadcast receiver does not work with this code.
I just want to call a method in action when I receive a broadcast request without creating an instance of the action. It can be done?
EDIT: Now I am adding code.
Here is my MainActivity class
public class MainActivity extends Activity {
public static final String TAG="App1";
private ActionReceiver receiver = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void printSomething(){
Toast.makeText(this,"Hello World",Toast.LENGTH_LONG);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is my ActionReceiver class, which is BroadcastReceiver
public class ActionReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if("com.example.app1.printSomething".equalsIgnoreCase(intent.getAction()))
Toast.makeText(context,"Hello World", Toast.LENGTH_LONG).show();
}
}
This is my file AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.app1.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=".ActionReceiver">
<intent-filter>
<action android:name="com.example.app1.printSomething"/>
</intent-filter>
</receiver>
</application>
</manifest>
, printSomething() , MainActivity onReceive() ActionReceiver . MainActivity . ?