How to get the proper actions from intent on android - android

There are two applications : Caller, Callee
Case 1 >
When I launch Callee application from home screen, I can get an action("android.intent.action.MAIN") from INTENT.
While Callee is running, I launch Callee application with an action("andoird.intent.action.test") again from Caller application. Then I get the action("android.intent.action.MAIN"). not action("andoird.intent.action.test").
How can I get the action("andoird.intent.action.test")?
Case 2 >
When I launch Callee application with an action("andoird.intent.action.test"), I get the action("andoird.intent.action.test").
While Callee is running, I launch Callee application from the home screen. Then I get the action("andoird.intent.action.test"). not action("android.intent.action.Main").
How can I get the action("android.intent.action.Main")?
What am I missing to get the proper action?
Here is Caller code below.
public class CallerActivity extends AppCompatActivity {
public static final String ACTION_TEST = "android.intent.action.test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test = (Button) findViewById(R.id.gotoSetting);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = getPackageManager().getLaunchIntentForPackage(PACKAGE);
intent.setAction(ACTION_TEST);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
});
}
}
Here is Callee code below.
public class CalleeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("test", "onCreate()");
Log.e("test", getIntent().getAction());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e("test", "onNewIntent()");
Log.e("test", getIntent().getAction());
}
}
Here is Callee's AndroidManifest.
<activity
android:name=".CalleeActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.test"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Please help.

Replace this code:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e("test", "onNewIntent()");
Log.e("test", getIntent().getAction());// getIntent() will return old intent
}
with
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e("test", "onNewIntent()");
Log.e("test", intent.getAction()); //use new intent
}

Related

Recreate an activity and also pass arguments

I have an activity that listens to preference changes and reloads the app. I am using recreate() to do that. But I don't know how to pass in arguments through that, so I have resorted to manual activity reloading.
Intent intent = getIntent();
finish();
// add in the arguments as Extras to the intent
startActivity(intent);
This has the behaviour I want, but the recreating of the activity isn't smooth for the user as they will see the activity being killed and the same activity relaunching. I want the user to not be aware that the activity was relaunched. So, my question is can I use the method recreate() and still pass arguments through it.
You can set the data on the activity's intent before calling recreate
getIntent().putExtra("RECREATE_DATA", "Some Data");
recreate()
since when you recreate the activity the same activity instance is used, the data in the intent will still be there after recreate.
You can try this way:
You can restart you activity with launch Mode as SingleTop and handle the onNewIntent(Intent intent) method. This way you are restarting the activity and send the intent, along with this activity is not being Killed i.e. oncreate of your activity will not be called.
public class MainActivity extends Activity implements View.OnClickListener {
Button btn ;
String mRelaunchData ;
public static String TAG = "RelaunchMainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(this);
Log.e(TAG, "onCreate called");
}
#Override
public void onClick(View view) {
Log.e(TAG, "onClick called");
Intent intent = new Intent("relaunch.activity.ACTIVITY_SELF_START_INTENT").setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("RESTART_DATA", "This is relaunch of this Activity");
startActivity(intent);
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e(TAG, "onNewIntent called");
mRelaunchData = intent.getStringExtra("RESTART_DATA");
Log.e(TAG, "mRelaunchData =" + mRelaunchData);
}
#Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume called");
if(mRelaunchData != null){
Toast.makeText(MainActivity.this, mRelaunchData, Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause called");
}
#Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart called");
}
#Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy called");
}
}
in AndroidManifest.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="relaunch.activity.ACTIVITY_SELF_START_INTENT" />
<category android:name = "android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
onClick will relaunch the Activity.
LifeCycle will be
-onclick
-onPause
-onNewIntent
-onResume

BroadcastReceiver registered in a activity never get called

I have 2 Activity one is sending a broadcust and another receiving. But receiver never get called -
MainActivity
public class MainActivity extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
this.sendBroadcast(broadcast);
}
}
ToastDisplay
public class ToastDisplay extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_display);
IntentFilter filter = new IntentFilter(BROADCAST_ACTION);
this.registerReceiver(new Receiver(), filter);
}
private class Receiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.d("TAG", "onReceive: SMS SENT!!");
Toast.makeText(getApplicationContext(), "SMS SENT!!", Toast.LENGTH_SHORT).show();
}
}
}
Manifest
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ToastDisplay">
<intent-filter>
<action android:name="com.aj.SHOWTOAST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can put broad cast receiver in your by this way also.
public class SampleActivity extends AppCompatActivity {
public static final String DISPLAY_MESSAGE_ACTION = "com.google.android.gcm.demo.app.DISPLAY_MESSAGE";
// Receive Message through Broadcast Receiver...
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String receiver = intent.getStringExtra("receiver");
Toast.makeText(context, "" + receiver, Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mobile_verify);
}
#Override
protected void onResume() {
super.onResume();
//registering the receiver...
registerReceiver(mHandleMessageReceiver, new IntentFilter(DISPLAY_MESSAGE_ACTION));
}
#Override
protected void onPause() {
super.onPause();
//Unregistering the receiver...
unregisterReceiver(mHandleMessageReceiver);
}
}
Broadcast receiver can be called through the following intent. You can use this intent any where to call this broadcast receiver. For example there are two activity and in those two activities you are using this receiver and you calling this intent from some other class which handles server response or database result. Whichever activity is in front that activities receiver will be called.i.e., activity one is in front if you are calling this receiver intent means then receiver in the activity one will be triggered.
Intent intent = new Intent(SampleActivity.DISPLAY_MESSAGE_ACTION);
intent.putExtra("receiver", "testMessage");
sendBroadcast(intent);
If the broadcast is going to happen within the app then it is good to use LocalBroadCastManager
public class MainActivity extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(broadcast);
}
}
public class ToastDisplay extends AppCompatActivity {
public static String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_display);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//your code here
}
};
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter(BROADCAST_ACTION)); }
}
You cant send boradcast one activity to another activity. So you have to do it like below.
First implement the Receiver as a separate class.
Receiver.java:
public class Receiver extends BroadcastReceiver {
public Receiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Hello", Toast.LENGTH_LONG).show();
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private static final String BROADCAST_ACTION = "com.aj.SHOWTOAST";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendBroadcast(View v) {
Intent broadcast = new Intent();
broadcast.setAction(BROADCAST_ACTION);
this.sendBroadcast(broadcast);
}
}
And manifest should be as below.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Receiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.aj.SHOWTOAST"/>
</intent-filter>
</receiver>

Why LocalBroadcastManager is not working?

I tried to learn LocalBroadcastManager. Searched in web and implemented a simple app with Two activity to perform LocalBroadcastManager. But it's not working. I can't found what's wrong with my code. please help me!
here is my code.
FirstActivity.java
public class FirstActivity extends Activity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
Intent intent = new Intent("localReceiver");
LocalBroadcastManager.getInstance(FirstActivity.this).sendBroadcast(intent);
}
});
} }
SecondActivity.java
public class SecondActivity extends Activity {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = (TextView) findViewById(R.id.hi_text);
LocalBroadcastManager.getInstance(this).registerReceiver(message, new IntentFilter("localReceiver"));
}
#Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(message);
}
private BroadcastReceiver message = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("SecondActivity", "Receiver Initiated...");
textView.setText("Intent receiver activated");
}
}; }
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:windowSoftInputMode="adjustResize|stateHidden"
android:label="SecondActivity" />
</application>
you are broadcasting something from FirstActivity, and you expect that SecondActivity receives it. In order this to happen you should be able to run two different Activitys at the same time, which is, by design, not possible. By the time you broadcast the string, you don't have anybody listening for it. Try using a Service to broadcast the String, with the receiving Activity resumed, and it will work
SecondActivity would looks like this:
private BroadcastReceiver uiUpdateReceiver;
onStart():
#Override
protected void onStart() {
LocalBroadcastManager.getInstance(SecondActivity.this).registerReceiver((uiUpdateReceiver), new IntentFilter("Your_Data");
super.onStart();
}
onCreate():
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
uiUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
}
onStop():
#Override
protected void onStop() {
LocalBroadcastManager.getInstance(ChatActivity.this).unregisterReceiver(uiUpdateReceiver);
super.onStop();
}
Hope it will help you.

Listening for ACTION_DREAMING_STOPPED

How would I get my app to listen for when DayDream stops. When the system stops dreaming it sends the ACTION_DREAMING_STOPPED string out. I have added a BroadcastReceiver in my OnResume and onCreate and neither are used when DayDream stops. So where should I put my listener? I do apologize if I am calling something by its wrong name, I haven't worked with DayDream before.
#Override
protected void onResume() {
mDreamingBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
// Resume the fragment as soon as the dreaming has
// stopped
Intent intent1 = new Intent(MainActivity.this, MainWelcome.class);
startActivity(intent1);
}
}
};
super.onResume();
}
The BroadcastReceiver can be created in your onCreate.
Ensure you register the receiver with: registerReceiver(receiver, filter) and that you've got the intent-filter inside your AndroidManifest.xml.
Sample:
MainActivity.java
public class MainActivity extends Activity
{
private static final String TAG = MainActivity.class.toString();
private BroadcastReceiver receiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, TAG + " received broacast intent: " + intent);
if (intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
Log.d(TAG, "received dream stopped");
}
}
};
filter = new IntentFilter("android.intent.action.DREAMING_STOPPED");
super.registerReceiver(receiver, filter);
}
}
AndroidManifest.xml
<activity
android:name="com.daydreamtester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.DREAMING_STOPPED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

BroadcastReceiver not firing

my receiver is not firing, code below:
AndroidManifest
<recevier android:name=".NoticeReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.clublifestyle.NoticeService.BROADCAST" />
</intent-filter>
</recevier>
NoticeReceiver.java
public class NoticeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "ASDASD", Toast.LENGTH_SHORT).show();
}
}
CLMainActivity.java
public class CLMainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.createTabs();
Intent i2 = new Intent(this, NoticeReceiver.class);
this.sendBroadcast(i2);
}
}
Can you help me to find out why?
Thanks!
Try to also set the action for the Intent i2:
Intent i2 = new Intent();
i2.setAction("com.clublifestyle.NoticeService.BROADCAST");
this.sendBroadcast(i2);
EDIT
There is a typo in your manifest. You have the <receiver> tag written as <recevier>. Your app sees no <receiver>

Categories

Resources