Unity - Android Broadcast receiver not been called - android-studio

I'm using Unity to set alarms for a project. (Usually wouldn't use Unity but it is a requirement for different reasons).
I've written an alarm like this, and this method is called from Unity with a parameter of 5:
public void SetAlarm (float time)
{
Intent intent = new Intent(context, AlarmClockPlugin.class);
intent.setAction("ALARM_INTENT");
PendingIntent pending = PendingIntent.getBroadcast(context,0,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (long) (1000 * time), pending);
}
This alarm class also inherits from BroadcastReceiver and overrides the OnReceive method like this:
#Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Write toast text for now
Toast.makeText(context,"Broadcast received",Toast.LENGTH_LONG).show();
wl.release();
}
I register the broadcast receiver in the manifest like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tombuild.myapp">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:label="#string/app_name"
android:supportsRtl="true">
<receiver android:process =":remote" android:name="com.tombuild.myapp.AlarmClockPlugin">
<intent-filter>
<action android:name="ALARM_INTENT"/>
</intent-filter>
</receiver>
</application>
</manifest>
But unfortunately the onReceive is not been called - no toast notification pops up at all after the 5 second duration, why is this?

I'm not familiar with Unity, but there obvious differences with Unity and Android Studio from the code you have posted, for example no Activity in the manifest. Why not try a simpler example in your setAlarm method.
...
Intent intent = new Intent();
intent.setAction("com.tombuild.myapp.ALARM_INTENT");
sendBroadcast(intent);
...
And just put a Toast in the onReceive method.
There is a very simple tutorial on BroadcastReceivers on TutorialsPoint, which got me started.

Related

Map Activity starting as Main Activity

I'm creating a simple app that consists in three (3) activities:
MainActivity -> For registering new users
LoginActivity -> For Sign in already registered users
HomeActivity -> Which for the moment shows a Google Map
I'd like to start the app with the MainActivity so in my Android Manifest I've defined that way:
<activity
android:name=".MapActivity"
android:label="#string/title_activity_map"/>
<activity android:name=".VerifyPhoneActivity" />
<activity android:name=".LoginActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
However, the problem is when I start the app automatically sends me to the HomeActivity (the Google Map Activity).
How could I solve this?
My HomeActivity.java:
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private FirebaseAuth auth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MapActivity.this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
Try manually editing your launching configurations. Go to run --> edit configurations and then go under launch options --> launch and choose specified activity. There, select MainActivity. Let me know if this works.

Fullscreen & Immersive Sticky mode. Android Studio and LibGDX

I am developing a game and I want it to be in Immersive sticky and Fullscreen mode, I used the code provided by the web page Android developers: https://developer.android.com/training/system-ui/immersive?hl=es_419#EnableFullscreen
Now, I implemented the code as follows(AndroidLauncher class):
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
}
}
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the navbar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
I used Android 9(API level 28) and technically it works, the phone's UI is hidden but there is a black space on top.
I want the app to fill the entire screen, but only the bottom part is filled while the top part is empty, I don't want the screen to scale when displaying phone UI.
I am using LibGDX and I don't know if it has something to do.
This is the AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:isGame="true"
android:appCategory="game"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="arrive.further.game.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
For those who haven't stumbled upon it yet, there's a simple way to achieve this in one line of code using the AndroidApplicationConfiguration object:
In your AndroidLauncher class:
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useImmersiveMode = true;
initialize(new YourGame(), config);
}
}

WebView in android studio activates the default browser on Samsung Tab S 8.4 SM-T700?

I am trying to build a simple app using WebView in android studio.
It runs okay in Nexus 5 emulator. However, when I run the same app on my real Samsung Tab S 8.4 SM-T700, the tablet asks me to select a default browser (using a dialog "open with Chrome or Internet"). And my app does not display the selected webpage.
How can I force android to run my app on Samsung Tab 8.4 S M-T700?
Here is the relevant code segment:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url= "http://www.google.com";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.loadUrl(url);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<uses-permission android:name="android.permission.INTERNET"/>
You need to add:
view.setWebViewClient(new WebViewClient());
before your call to loadUrl. Google does a redirect, and redirects should be handled by a WebViewClient. When it's not set, WebView passes the URL to the default browser. Setting an empty WebViewClient makes WebView to load the URL in itself.

Button labels get clipped if font size is set to small. What can I do about it?

There's a font setting in Android system settings. I've tested my layouts with all the options from default Normal up to Huge, but it didn't occur to me someone would use Small. Turns out, someone does.
My layouts use styles, and styles have some dimensions constrained to certain sp or dp values. It took a lot of trial and error to balance the values for all the screen and font sizes, and I would really like not having to tweak it all over again from scratch. Is there a way for my app to ignore the font size setting? Is there a way to say to Android that I don't want the app to be affected by options smaller than Normal?
Following is a little dirty, but worked on Nexus 5 (Android 4.4.2):
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<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"
android:configChanges="fontScale" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make sure to do this before setContentView()
Configuration currentConfig = getResources().getConfiguration();
if (currentConfig.fontScale < 1.0f) {
currentConfig.fontScale = 1.0f;
getResources().updateConfiguration(currentConfig, null);
}
setContentView(R.layout.activity_main);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.fontScale < 1.0f) {
newConfig.fontScale = 1.0f;
}
getResources().updateConfiguration(newConfig, null);
// Restart to apply the new changes
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
Calendar.getInstance().getTimeInMillis() + 1000, // one second
PendingIntent.getActivity(this, 0, getIntent(), PendingIntent.FLAG_ONE_SHOT
| PendingIntent.FLAG_CANCEL_CURRENT));
finish();
}
}
Hope this helps.
Please let me know if there is any need for explanation of the above.

i want to implement searchbar concept in my app

i want to implement searcb bar concept in my app .
when ever user clicks on searbar the keyboard buttons need to display with default searchable button in it ..i followed few of the links ...but unable to get the searchable bar nor the keyboard populated buttons ...
please help me thanks in advance .
this is my searchable.xml placed res/xml
<searchable
android:label="#string/app_label"
android:hint="#string/search_hint" >
</searchable>
and this is my manifest file
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".SearchboxActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
here is the activity ...
package com.hands;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
public class SearchboxActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// doMySearch(query);
}
}
private void doMySearch(String query) {
// TODO Auto-generated method stub
System.out.println("hello 2");
}
}

Resources