I'm creating a extremely simple app for Android 2.2: the only thing that does is to open an URL using the default browser.
When the app icon is clicked for a short time there is a black screen with the app name at the top before opening the browser.
Is this unavoidable when launching an app or is there any trick to make the app "silent", i.e., to jump instantly to the browser?
Here's what I've tried:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//setContentView(R.layout.activity_main); Don't even show layout
Uri uri = Uri.parse("https://www.google.com");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
finish();
}
}
Just to be on the safe side I also put android:visibility="gone" in activity_main.xml.
Thanks in advance!
Try using theme Theme.Translucent.NoTitleBar
Translucent makes you transparent by default, no title bar kills the title bar.
Related
I have a very simple activity, that redirects the user to the app's Play Store page, when the button is clicked:
public class MyActivity extends AppCompatActivity {
private static final String PLAY_STORE_URI =
"market://details?id=" + BuildConfig.APPLICATION_ID;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
findViewById(R.id.go_to_play_store).setOnClickListener(this::goToPlayStore);
}
public void goToPlayStore(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(PLAY_STORE_URI));
startActivity(intent);
}
}
Is it possible to write a test to check that the PlayStore is launched when the button is clicked? Better, is it possible to verify it shows the expected page?
I know that by using ActivityMonitors transitions between Activities can be tested. I also know that I can verify the Intents being sent using Espresso Intents. But can I actually check that a user action launches another app?
I would click the button, then use:
intended(allOf(
hasAction(Intent.ACTION_VIEW),
hasData("https://play.google.com/store/apps/...your app...")
))
I would suggest a slightly different question - is it you app's job to verify that? You'd be testing not your own app but Android OS and Google's Play Store App.
The way I've been approaching it is by being explicit about the boundaries, and aware of the possible scenarios.
What I mean by that is, extract the Intent manipulation and invocation logic into a thin service (that's the being explicit about boundaries part) and test your Activity correctly interacts with it (by constructing the Intent appropriately).
The part about possible scenarios is being aware of what can happen. For example what does your app do (if anything) if the user doesn't have Play Store on their phone.
I manage some events in a background BroadcastReceiver. In some specific situations I need to launch an activity. This activity is supposed to be hidden. Nothing should be displayed to the user.
It works well but if the user is using the keyboard, typing something, the keyboard is hidden. If the user is using GMail, writing an email, then a "draft" is saved.
It seems like this activity gets to the top and stops the one the user is using even if it is invisible. Is there any way to solve this?
This how I declare the activity in manifest:
<activity
android:name=".MyInvisbleActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
</activity>
This is how I launch it:
Intent intent = new Intent(MyContext, MyInvisbleActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyContext.startActivity(intent);
This is the MyInvisbleActivity onCreate():
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(---); //This is not used
Window oWindow = getWindow();
oWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
--do work--
}
Thank you!
Android provides a theme for this android:theme="#android:style/Theme.NoDisplay"
Alternatively it sounds like you might actually want a service. But youd need to provide more information on what you are trying to achieve
Please have a look at the attached image it shows screen that i want for my activity. I want to stop user from proceeding further until he enters correct password. I want to this screen to come up when my phone boots after restarting.
I have written listener for listener device boot process.
public class DeviceStartUpListener extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
// some code
Intent authenticationIntent = new Intent(applicationContext, PostStartupAuthenticationActivity.class);
authenticationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(authenticationIntent);
}
}
}
I want my PostStartupAuthenticationActivity to cover complete screen and ask user for password. This activity should hide all the button at bottom of screen. Your should not be able to move to any other activity until he enters correct password or until he enters wrong password for consecutive 3 times.
In PostStartupAuthenticationActivity i have written following code in onCreate method, but this is not working correctly, The activity is displayed full screen but the button at the bottom of screen are still visible eg home button and back button are still visible, I want activity to open in full screen mode but no button should visible at the bottom of screen, My present activity is looking like this, please find second attached image:-
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mIntent = getIntent();
//set fullscreen and no title
// requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.post_starup_authentication_activity);
}
Thanks in advance.
If you are trying to make app which gonna lock the device by password - you not gonna achive it and your attempt to do it is useless. User always could press the hadware button "Home" and your app will gone on the background. But if you want only to prevent user to use your particular app without passing the password screen - just dont start any activity before user enters correct password like:
if (entered_password_is_correct())
start_next_corresponding_activity();
I've got a tab layout in my app. My last tab is only needed to display an other app that is installed on my tablet. This app isn't mine. I've installed it from the market.
At the moment I can touch on the tab and the other app opens in a new page/view in fullscreen. But I don't want to leave my app. The app should be nestet in my app so that I the othe app is running under the tab bar of my app.
Code that opens other app via intent:
public class MyActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getPackageManager().getLaunchIntentForPackage("org.kman.WifiManager");
startActivity(i);
}
}
the part of tab layout:
spec = tabHost.newTabSpec("mytab").setIndicator("MyTab",
res.getDrawable(R.drawable.address_book_new))
.setContent(new Intent(this, MyActivity.class));
tabHost.addTab(spec);
Any hints how to display the app in the tab host?
Thx B770
Even if the other app is launched, the user is not ’leaving' your app. That's the whole point of the activity stack and the back button. The user knows your app is just a back button away.
I don't think it is possible what you ask. I wouldn't like it as a user. The other app developer would not have anticipated a non-full-screen layout. For the OS it creates all sorts of event handling challenges too.
I created an app to simply open a web browser and go to a specific website. However, after it's run for the first time, if I go to another app and run the app again, I will get nothing except the black screen. I knew I can find the page from web browser, however I wonder if there is a way to bring the content back when I launch the app and the user could get back to work from where he/she stopped.
here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** setup the url and start a browser with
* the hotmial.com and return to the user
* */
String url = "http://www.hotmail.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
Thanks guys
Take a look at the activity lifecycle. You may want to move your intent to OnStart() for when the application comes back to the foreground after being stopped.
The activity may still exist when you run your app a second time; perhaps onCreate is not called.
After you throw the Intent to run the browser, shut down the activity so that onCreate happens every time.
If your application is here only to give a link to a website, this is much overhead in my opinion. Android users can make a shortcut to any website they want on the desktop, the shortcut icon being shown with the site's favicon in its corner.
Try this if you want to open the browser OUTSIDE your app. I know it seems like a simple(and trivial) edit, but I've never had issues setting it up this way.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** setup the url and start a browser with
* the hotmial.com and return to the user
* */
String url = "http://www.hotmail.com";
Uri uri = Uri.parse(url);
Intent i= new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
}
Why don't you use the android.webkit.WebView which will allow you to directly browse web pages in your app. Here the web content will remain persistent as long as the application is running. On the chance that your application stops you could save the url using WebView.getURL() which would allow you to continue from where you left of the next time you start your app. If this is what you want to do let me know and I will post some sample code.