android dev {Intent} - java

public void sendCustomIntent(View view) {
Intent intent = new Intent();
intent.setAction("com.examole.intentnbroadcast.CUSTOM_INTENT");
intent.putExtra("message", "Custom Intent Test");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
}
Hi, i'm a begineer in android dev.
I try to use image button to connect this method but i can't totally understand this code.
I click the button of this method but nothing happend, can someone explain it to me, thanks!!
Now, i'm not sure the code of this, what's its functionality?
intent.setAction("com.examole.intentnbroadcast.CUSTOM_INTENT");

Related

Is there a method to call activity back when onStop() immediately?

I want to create an app that can't be stopped (send to background). Is there a method to achieve this? this is my code
#Override
protected void onUserLeaveHint()
{
Intent i = new Intent(this, DemoActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT & Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(i);
}
but this code didnt respond immediately, I search everywehere for solution but get nothing. Thank you.

error in implimantation of intent

I have written the following code in java file in eclipse to make launch of snapchat application on my android device through my application. But when i am running it in my android device it shows "no apps can perform this action".
if(view.getId()==R.id.LaunchSnapchat){
intent= new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("snapchat://details?id=com.snapchat.android&hl=en&rdid=com.snapchat.android"));
chooser=Intent.createChooser(intent,"Launch Snapchat");
startActivity(chooser);
}
what is the solution??
Simple answer, as taken from here
If you don't know the main activity, then the package name can be used to launch the application.
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.snapchat.android");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
}
Try this
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.setPackage("com.snapchat.android");
startActivity(Intent.createChooser(intent, "Open Snapchat"));
Also work this it is because mabe you did not have snapchat installed (emulator)
if(view.getId()==R.id.LaunchSnapchat){
intent= new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("snapchat://details?id=com.snapchat.android&hl=en&rdid=com.snapchat.android"));
chooser=Intent.createChooser(intent,"Launch Snapchat");
startActivity(chooser);
}

Android Implicit Intent for Viewing a Video File

In my Android app, I have a button that when clicked, launches the external application of my choice to play a video (I gather that this is called an "implicit intent"). Here is the relevant Java code from my onCreate method.
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(i);
}
}
);
I expected this to work, since I've followed tutorials and the Android developers documentation pretty closely, but when I test my app in the AVD, instead of prompting a menu of external applications where I can view my video, the app crashes.
What is causing my app to crash?
Change your onClick method to below code. You should give the option to choose the external player.
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}
Change your code to add this check:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
// Check there is an activity that can handle this intent
if (i.resolveActivity(getPackageManager()) == null) {
// TODO No activity available. Do something else.
} else {
startActivity(i);
}

How to close an activity (finish()) from service?

i make a lock screen application.. i have some service that listen to the SMS.. when the SMS receiving command andro-lock then i display a lock screen that called by service:
here'e the service's code :
if (tempMessage[0].toString().equalsIgnoreCase("andro-lock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//Toast.makeText(ListenSMSservice.this, "Menjalankan command andro-lock", Toast.LENGTH_LONG).show();
openDatabase();
updateStatusL();
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
}
that code has works perfecly, but i have some problem when i try to unlock my phone.. if my service receive command andro-unlock then i must close (finish) that lockscreen.java.. i was trying many code and still not works.. what must i do to close the lockscreen activity when my service receiving command andro-unlock?? please help..
else if (tempMessage[0].toString().equalsIgnoreCase("andro-unlock") && tempMessage[1].toString().equals(tempPassword.toString()))
{
//what must i do??
//any solution??
}
Thanks for your help..
A possible solution to stop the activity would be:
Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle myKillerBundle = new Bundle();
myKillerBundle.putInt("kill",1);
myIntent.putExtras(myKillerBundle);
getApplication().startActivity(myIntent);
In LockScreenForm
onCreate(Bundle bundle){
if(this.getIntent().getExtras().getInt("kill")==1)
finish();
}
You could send an intent to your activity with an extra, and the activity could read this extra and, if present, finish() itself.
Regards,
Stéphane

How to clear an activity from the second activity going back to home screen in android?

example scenario is:
from login screen - main screen - then when i clicked a hide button the app will go to home screen, and when im going to click the app again the main screen would be called
Fire an intent when you want to display the home screen
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
So this will be fired on the pressing of your hide button
I think you can use you can use FLAG_ACTIVITY_CLEAR_TOP
FirstActivity is the first activity in the application:
public static void home(Context ctx) {
if (!(ctx instanceof FirstActivity)) {
Intent intent = new Intent(ctx, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity(intent);
}
}
And If you want to exit from the whole application,this help you in that.
public static void clearAndExit(Context ctx) {
if (!(ctx instanceof FirstActivity)) {
Intent intent = new Intent(ctx, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Bundle bundle = new Bundle();
bundle.putBoolean("exit", true);
intent.putExtras(bundle);
ctx.startActivity(intent);
} else {
((Activity) ctx).finish();
}
}
i Really Hope this helps.

Categories

Resources