For the first time im trying to set a landscape orientation on my app. Im having so much problems cause it uses actionbar.tab's and service and i dont know very well yet how all of this will happens on each fragment specifically. The first problem i faced was the asynchrony between activity lifecycle and service, cause the service was unbind when the activity was already recreated. Then i tried to set android:configChanges="orientation" on my manifest to manually handle the events, but the activity stills recreating itself.
I dont know how i will handle so many details on orientation changes, the most of data and information i have to retrieve is on service, it guards my app state, but im not getting do it.
Please help me before i give up implementing this funcion on my app.
I will post some code.
This is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.irclient2"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.irclient2.activity.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.irclient2.service.MyService"
android:exported="false" >
</service>
<!-- AS DUAS TAGS SEGUINTES SÃO APENAS PARA O FUNCIONAMENTO DO ADMOB -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
This is my activity:
public class MainActivity extends ActionBarActivity implements
ServiceConnection {
#Override
protected void onCreate(Bundle savedInstanceState) {
MyService.log("Activity onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerlayout.setDrawerShadow(R.drawable.drawer_shadow, Gravity.LEFT);
registerReceivers();
cancelaNotificacao(R.drawable.ic_launcher);
Intent it = new Intent(this, MyService.class);
if (!MyService.RUNNING) {
startService(it);
}
if (!bindService(it, this, 0)) {
mensagemErro("Erro!",
"Não foi possível conectar ao service. Fechando app.");
finish();
}
}
#Override
protected void onDestroy() {
MyService.log("Activity onDestroy()");
unregisterReceivers();
unbindService(this);
super.onDestroy();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// /////////////////////// CASO PORTRAIT ///////////////////////////
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
// /////////////////////// CASO LANDSCAPE ///////////////////////////
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
}
}
#Override
public void onServiceConnected(ComponentName name, IBinder binderservice) {
MyService.log("Activity onServiceConnected");
LocalBinder binder = (LocalBinder) binderservice;
this.service = binder.getService();
carregaRadio();
if (MyService.bot != null && MyService.bot.isConnected()) {
carregaChatdoService();
selectTab(TITLE_TAB_CHAT);
} else {
carregaLogin();
selectTab(TITLE_TAB_RADIO);
}
}
#Override
public void onServiceDisconnected(ComponentName name) {
service = null;
}
//////////// THERE ARE MORE THINGS //////////
My RelativeLayout that is the custom view for the actionbar.tabs:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_marginLeft="-3dip"
android:layout_marginRight="-3dip"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/tabtitle"
style="?android:attr/tabWidgetStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:text="Tab Title" />
</RelativeLayout>
Due to my activity always get the data from service, i though it would be easilly with activity being recreated, but i had problems with service connection during the process because service bind and unbind are called and assynchronous.
As i already said, is my first time doing it, please tell me what is best thing to do.
Thanks in advance.
You can use this line of code , in your activity , before the setContentView(R.layout.yourlayout) ;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) ;
and for the android 3.2+ , you should also add this :
android:configChanges="orientation|screenSize"
And study the documentation here!
Related
None of the many similar questions I have found have helped to solve my issue.
Here are some questions I've looked at:
ServiceConnection.onServiceConnected() never called after binding to started service
onServiceConnected never called after bindService method
ServiceConnection::onServiceConnected not called even though Context::bindService returns true?
OnServiceConnected not getting called
Here is part of my android app code:
//Local service interface
private INetworkQueryService myNetworkQueryService = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Intent intent = new Intent(this, NetworkQueryService.class);
startService (intent);
bindService (new Intent(this, NetworkQueryService.class), myNetworkQueryServiceConnection,
Context.BIND_AUTO_CREATE);
}
//Service connection
private final ServiceConnection myNetworkQueryServiceConnection = new ServiceConnection() {
/** Handle the task of binding the local object to the service */
public void onServiceConnected(ComponentName className, IBinder service) {
myNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
// as soon as it is bound, run a query.
loadNetworksList();
}
/** Handle the task of cleaning up the local binding */
public void onServiceDisconnected(ComponentName className) {
myNetworkQueryService = null;
}
public void loadNetworksList() {
// delegate query request to the service.
try {
myNetworkQueryService.startNetworkQuery(myCallback);
} catch (RemoteException e) {
}
}
};
/**
* This implementation of INetworkQueryServiceCallback is used to receive
* callback notifications from the network query service.
*/
private final INetworkQueryServiceCallback myCallback = new INetworkQueryServiceCallback.Stub() {
#Override
public void onQueryComplete(List<NetworkInfo> networkInfoArray,
int status) throws RemoteException {
displayNetworks(networkInfoArray, status);
}
/** place the message on the looper queue upon query completion. */
};
As you can sy, myNetworkQueryServiceConnection should be called from within the bindService method (located in onCreate()). This (in theory) should also run onServiceConnected (I think), but I put a breakpoint in my code and it never stops inside that method. I did some debugging and bindService returns false, so for some reason it's not successfully binding it.
This is my first time ever having to bind services in Android, so I'm sure I've missed something. I'm hoping someone could point out a mistake.
Let me know if you're missing any information--I'm not sure what all you might need, and I really can't post my entire app code here.
EDIT: I keep reading things about registering the service in the manifest but cannot seem to figure out where or how that would be done? Here's the manifest for this project:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
</application>
I certainly don't see anything about the service, but I guess I don't know how to add it.
You need to add a <service> element, as a peer of your <activity> element, as a child of <application>:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<service android:name="NetworkQueryService" />
</application>
My example element assumes that NetworkQueryService is really com.tek15.cellemrmonitor.NetworkQueryService — otherwise, substitute in the proper fully-qualified class name.
I have no idea what it could be. I searched through my code for like 30 min
MainActivity:
public class MainActivity extends Activity {
public void onResume(){
ArrayList<String> voiceResults = getIntent().getExtras()
.getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
if (voiceResults.size() >= 1) {
String infoId = "That is what you said";
Card ShowDataCard = new Card(this);
ShowDataCard.setText(voiceResults.get(0));
//ShowDataCard.setText("Testing");
ShowDataCard.setInfo(infoId);
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
else {
String mainText = "You did not say anything.";
String infoId = "Why didn't you say anything?";
Card ShowDataCard = new Card(this);
ShowDataCard.setText(mainText);
ShowDataCard.setInfo(infoId);
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.glass.texttospeech"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.example.glass.texttospeach.MainActivity"
android:label="#string/app_name"
android:icon="#drawable/icon" >
<intent-filter>
<action android:name="com.google.android.glass.action.VOICE_TRIGGER" />
</intent-filter>
<meta-data android:name="com.google.android.glass.VoiceTrigger"
android:resource="#xml/voice_trigger_start" />
</activity>
</application>
So what could it be? The intent filters are correct, the packages are correct, voice trigger is correct.
I am just trying to have it print what you said. I have done it before but it will not work now!
when you are overriding Activity methods, don't forget to call super methods.
here you are overriding onResume() but not calling super method. call super method like
#Override
public void onResume() {
super.onResume();
// do your work....
}
and use Override annotation when you are overriding any method for readability and check for NullPointException when you are getting extras from Intent. Intent.getExtras...() will return null if there is no mapping found for a given key
I m trying to start an IntentService from the main activity of y application and it won't start. I have the service in the manifest file. Here's the code:
MainActivity
public class Home extends Activity {
private LinearLayout kontejner;
IntentFilter intentFilter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
kontejner = (LinearLayout) findViewById(R.id.kontejner);
intentFilter = new IntentFilter();
startService(new Intent(getBaseContext(), HomeService.class));
}
}
Service:
public class HomeService extends IntentService {
public HomeService() {
super("HomeService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(), "TEST", Toast.LENGTH_LONG).show();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salefinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Home"
android:label="#string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".HomeService" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
How can I make it work?
onHandleIntent gets called from a background thread. You can't modify the UI, or in this case, make Toast from outside the UI thread. So, I wouldn't expect anything to happen with your service.
Just try writing something out with Log.d() to see if your service is getting called.
It seams that android cached a bad version of the app - I forced closed it and started it again, and it worked...
Probably an easy question for you guys, but its my first attempt at creating a service that should run in the background when my app closes. My problem is: The service doesnt start when I click on my "start service" button. I don't see any of the toast, and nothing in the logcat (no errors either). Thanks in advance!
Service class
public class MyService extends Service {
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
My main activity
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.johanberntsson.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<application
android:icon="#drawable/maps1"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".LongitudeActivity"
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=".DebugActivity" />
<activity android:name=".GoogleMapsActivity" />
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
this is suggestion as all else looking fine
should call super in each function like super.onCreate();
as may be possible service is already started and try to stop and start again and check it toast appears ....
try
<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" />
You may have already figured this out. In your code you override several native functions so you could add a toast and a catlog entry. Don't forget to add super.{whatever function you are calling}. What that does is it tells the android device to do whatever the function would have done had the function not been overrided, then do the extra stuff. In your case, if you call your oncreate function, it then the computer will make the toast and add a catlog entry, but it will not create the service.
#Override //forget about doing whatever onCreate usually does.
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
//note: Service not created because function was overrided
}
however, if you add super.onCreate, the phone will create the service, then it will do what you tell it to do.
#Override //forget about doing whatever onCreate usually does.
public void onCreate() {
super.onCreate(); // do whatever onCreate usually does. Then do whatever is after me.
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
}
I hope this helps.
I added your service into my code and it is working fine for me. please see below code and match with yours.
Activity >>
public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button)findViewById(R.id.button11);
bt.setOnClickListener(this);
}
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
#Override
public void onClick(View v) {
startServiceClick(v);
}
Manifest File >>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".MyService" />
</application>
I am developing an Android app, in which I have to move to MapActivity on Button click event.
But the app is crashing while switching from Activity to MapActivity.
can anyone help me on this?
Thanks in advance.
Here is my code,
public class PopupActivity extends Activity implements GPSCallback
{
.......
public void display_map(String str)
{
Intent showMap_intent = new Intent(this,DisplayGoogleMaps.class);
PopupActivity.this.startActivity(showMap_intent);
}
}
This is my map Activity class
public class DisplayGoogleMaps extends MapActivity
{
MapView mapView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
}
}
And This is my Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxxx"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:label="#string/app_name"
android:name=".PopupActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".DisplayGoogleMaps" >
</activity>
</application>
</manifest>
there is nothing in error log while crashing!
Did you declare the map activity in your Manifest? Did you set the Android API package as Google APIs ?
These are mandatory.
What do you mean with "while switching from Activity to MapActivity"?
How do you switch? Any source code?
When you want to show some data in am MapView you have to start an Intent.
public void onClick(View view) {
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
// Set the request code to any code you like, you can identify the
// callback via this code
startActivity(i);
}
From http://www.vogella.de/articles/AndroidIntent/article.html => 6. Tutorial: Explicit intents and data transfer between activities
In your case the class >ActivityTwo< has to be the MapActivity