The Android app I am building uses a library that is constantly pushing data to a remote server. In a demo MyMainactivity did exactly this without any user interface and the activity worked well.
Now that I am building the UI around this activity with a TabView I am puzzled how to execute MyMainActivity - in my manifest I now have MyTabActivity as LAUNCHER so how can I make MyMainActivity and MyTabActivity both start up on launch? (MyMainActivity should run while the user is able to scroll through the tabs ans at a later stage should have influence on how MyMainActivity sends data to our servers).
<activity
android:name=".MyTabActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can put the MyMainActivity in the first visible tab, so that it gets started when the tab is becoming visible.
Or you can launch MyMainActivity start what you need to do and then forward your app to the MyTabActivity.
Every activity with a GUI has an onCreate method that you override to define what happens when the activity is launched. You would add the code to run background service when MyTabActivity is created.
class MyTabActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
Intent myIntent = new Intent(getApplicationContext(), MyMainActivity.class);
startService(myIntent);
setContentView(viewid);
}
}
You can try by this.
<activity
android:name=".MyTabActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name=".MyMainactivity " >
</activity>
</activity>
Another way you also try by this way in your tab activity. Here, you use your stuff.
<receiver android:name=".AutoConnection" >
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver
public class AutoConnection extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Related
I've added a new Activity, but the Toast message doesn't ever appear. It's not Toast specific though, it doesn't reach this class in general.
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This should appear
Toast(..., "Inside of SecondActivity onCreate", ...).show();
}
...
My AndroidManifest looks like this,
<application
...
<activity
android:name=".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>
<activity android:name=".SecondActivity" ></activity>
</application>
you never call show() on your Toast
the launcher activity is MainActivity, but the Toast is inside SecondActivity. Did you add the logic to launch SecondActivity?
Edit:
From your Manifest I can read
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
So, when you execute your Application, the first activity launched will be MainActivity. Inside it you need to launch SecondActivity. For instance inside MainActivity's onCreate you could have something like:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
My application works as a launcher and also it starts on startup. However, something is wrong with it. For instance, I install my application on device, and open it by selecting Always button (as default launcher). There is no problem until here. However, if I reboot my device (it opens on startup, as I said before), the application opens. But when I want to close it, I cannot do that. It opens again.
This is my Manifest file:
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="com.comeks.cocuktablet.Main"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is BootUpReceiver.java:
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
You can use the PackageManager to clear your own application of its defaults, put this inside your onCreate():
PackageManager pm = getPackageManager();
pm.clearPackagePreferredActivities("com.your.package.name");
and fill in your own package name. That should clear the launcher default from your app, the next time they press home button they should be shown the choices of which app to use.
I've written following class to start my application activity Home.class but on device start up it shows error forced close.
public class MyBootRecvr extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Home.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_FROM_BACKGROUND);
context.startActivity(i);
Toast.makeText(context, "Where is my KeyBoard", Toast.LENGTH_LONG)
.show();
}
}
Permissions and receiver tags in application.
<receiver
android:name=".Home"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
If this is the only code in your application, it will never run. Android applications must have at least one subclass of Activity as a starting point for when you run the app. The Activity could should at a minimum look something like this:
class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.my_layout); /* my_layout should be an XML layout in res/layout */
}
}
Make sure the following code in is your AndroidManifest.xml file
<activity android:name="MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you create a new Android project in Eclipse, it will do this setting up for you. There are a lot of tutorials on both setting up a basic application in Android, and using Eclipse to do it for you.
doing this solve the problem
thanks to xono
<receiver
android:name=".MyBootRecvr"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
I have an application that has one service and 2 activities.One activity (the main one) is a preferences activity and the other one is a dialog themed activity. The service from time to time needs to open only the dialog themed activity (using FLAG_ACTIVITY_NEW_TASK).
The problem is that when the preferences activity is opened in background (for example the user pressed HOME key instead of BACK, so the activity is OnPause) and the service tries to open the dialog themed activity, also the main activity is opened (comes in foreground).
I don't want this. So, how can I open only the themed activity from the service, without the main activity pop up ?
Android Manifest .xml
<service android:name=".SimpleService"> </service>
<activity
android:name=".Preferences" 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=".popup_activity" android:theme="#android:style/Theme.Dialog" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="screenon.popup.activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And from the service :
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory("screenon.popup.activity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I believe you're looking for the android:launchMode attribute.
Alex , I believe that all you want is to display preferences activity just once & then it must die(or lets say finish()) .
In that case you can override onResume of the preferences activity
to finish your preferences activity after it is resumed after pause
#Override
public void onResume()
{
//if activity is resumed after onPause then only run it
this.finish(); //simply kill your activity if it is resumed after pause
}
EDIT-To know whether the activity was paused you need to override onPause().I think you can dig out the rest.
Hope it helps!
I have activity A in package one, and I want to run an intent which will up an activity B which is in package two.
How can I do this? Any samples will be welcome.
this is what ive done, and the error i get:
first activity ("MainActivity") in a package: com.abelski.currencyclient
and second activity("SecondActivity" in a diffrent package: com.idan.second
now i wanna call from MainActivity to SecondActivity.
ive added this line at the manifest of the MainActivity:
<activity android:name="com.idan.second.SecondApplicationActivity"></activity>
now in main Activity i got this button which run this line:
Intent intent = new Intent(MainActivity.this,SecondApplicationActivity.class);
and this is the rror:
04-29 09:20:59.197: ERROR/AndroidRuntime(399): Uncaught handler: thread main exiting due to uncaught exception
04-29 09:20:59.276: ERROR/AndroidRuntime(399): java.lang.NoClassDefFoundError: com.idan.second.SecondApplicationActivity
04-29 09:20:59.276: ERROR/AndroidRuntime(399):
I am assuming that by "packages" you mean applications.
We have:
- ApplicationA with FirstActivity
- ApplicationB with SecondActivity
If, in the ApplicationB's AndroidManifest.xml file, in the declaration of SecondActivity you add an intent filter such as:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="applicationB.intent.action.Launch" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can create an Intent to launch this SecondActivity from the FirstActivity with:
Intent intent = new Intent("applicationB.intent.action.Launch");
startActivity(intent);
What this all means is:
The SecondActivity has a filter for the intent action of "applicationB.intent.action.Launch"
When you create an intent with that action and call 'startActivity' the system will find the activity (if any) that responds to it
The documentation for this is at: https://developer.android.com/reference/android/content/Intent.html
I had the same problem and the solution was another level in the root of your package name.
If you have two packages "com.first...." and "com.second...", and the manifest is referencing "com.first". Then you can refactor both packages in order to reuse the first part. For instance, "com.package.first" and "com.package.second". Then your manifest has to be also updated
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
...
<activity android:name=".first.FirstPackageActivity" android:label="FirstPackageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".second.SecondPackageActivity" android:label="SecondPackageActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Your java code can create an intent and start the activity in the usual way:
Intent intent = new Intent(this,ActivityClassName.class);
startActivity(intent);
If the package you mentioned here is same to application,I think the answer in the question Android: Starting An Activity For A Different Third Party App is simpler。With the first answer to that question, you don't need to make any modification to your second application.
Use explicit intents:
Intent intent = new Intent(context,ClassName.class);
where ClassName is from another package.
Sometimes, you will not know the name of the class in such cases you will have to rely on the Intent that the target class advertises to handle.
First, you need to declare both packages and activities on Manifest :
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and for the second activities, on android:name --> .Packages name.activity, assuming your second packages name com.iden.second :
<activity android:name=".second.SecondActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<intent-filter>
<action android:name="android.intent.action.SECOND" />
<category android:name="android.intent.category.SECOND" />
</intent-filter>
</activity>
then on the MAINACTIVITY JAVA CLASS, asuming you will start second activity using a button :
public class MainActivity extends AppCompatActivity {
private Button mButtonSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSecond = findViewById(R.id.btn_second);
mButtonSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent newActivity = new Intent(MainActivity.this,com.iden.second.SECOND.class);
startActivity(newActivity);
}
});
}
}
hope that can help, since i am not clear with the question structure