I'm very new to android studio development so the answer is probably trivial.
That being said, I've busted my head around the problem for a day now :/
I am trying to run a chunk of code every 10 seconds even when the app is closed using AlarmManager.
I copy-pasted what is suggested in this guide https://guides.codepath.com/android/Starting-Background-Services
I'm a little puzzled by the result -
I think I do get alarm triggered every 10 seconds, as indicated in the log:
04-20 21:25:44.125 557-632/? D/AlarmManager: Triggered Alarm 22ad4130
RTC_WAKEUP IntentSender{220fb650: PendingIntentRecord{2243b570
com.example.somelocation.myapplication broadcastIntent}} 04-20
21:25:44.125 557-557/? V/AlarmManager: triggered: flg=0x14
cmp=com.example.somelocation.myapplication/.MyAlarmReceiver
But i can't see any of my custom logs positioned inside my BroadcastReceiver or my IntentService.
MyTestService:
package com.example.somelocation.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class MyTestService extends IntentService {
public MyTestService() {
super("MyTestService");
}
#Override
protected void onHandleIntent(Intent intent) {
// Do the task here
Log.i("MyTestService", "Service running"); // Can't see this log
}
}
MyAlarmReceiver:
package com.example.somelocation.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyAlarmReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;
#Override
public void onReceive(Context context, Intent intent) {
Log.e("MyTestService","broadcastreciever onReceive called"); // Can't see this log
Intent i = new Intent(context, MyTestService.class);
i.putExtra("foo", "bar");
context.startService(i);
}
}
Main Activity:
package com.example.somelocation.myapplication;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView batteryInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scheduleAlarm();
}
public void scheduleAlarm() {
Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
10000, pIntent);
}
}
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.somelocation.myapplication">
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver
android:name=".MyAlarmReceiver"
android:process=":remote" >
</receiver>
<service
android:name=".MyTestService"
android:exported="false"/>
</activity>
</application>
</manifest>
What am I missing?
update:
Got it! It was an error in my manifest file:
The close tag of the main activity was after the receiver declaration. should be:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyAlarmReceiver"
android:process=":remote" >
</receiver>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true"/>
</application>
The log is being printed on the console... here is the log I am getting
04-22 18:30:28.421 1830-4146/com.example.somelocation.myapplication I/MyTestService: Service running
04-22 18:30:45.337 2641-2641/com.example.somelocation.myapplication:remote E/MyTestService: broadcastreciever onReceive called
04-22 18:30:45.344 1830-4394/com.example.somelocation.myapplication I/MyTestService: Service running
04-22 18:30:48.4032641-2641/com.example.somelocation.myapplication:remote E/MyTestService: broadcastreciever onReceive called
You are creating a separate process('remote') for the BroadcastReceiver and your logcat may be setup to display only the main process create at first.
See the screenshots -
So change the application process from the dropdown and also select the log level to Verbose.
Related
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.
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);
}
}
Hi guys Im trying to make a push notif using alarmmanager
for my simple voting system.
The problem is no notification showing.
whats wrong with this?
Also i notice that my mBuilder = new NotificationCompat.Builder(context)
has warning says "Builder is deprecated" is that the reason why its not working?
this is my codes
AlerReciever class
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import. android.support.v4.app.
NotificationCompat;
/**
* Created by ivan on 2/27/2018.
*/
public class AlertReceiver extends. B.
BroadcastReceiver {
#Override
public void onReceive(
Context context, Intent. intent) {
createNotification(context,"Voting. System","
Congrats to winner","Alert" );
}
public void createNotification(Context context,
String msg
,String msgText, String msgAlert){
PendingIntent notificIntent =
PendingIntent.getActivity(context,0,
new Intent(context,
AddCandidate.class),0);
NotificationCompat.Builder mBuilder = new.
NotificationCompat.Builder(context)
.setSmallIcon(R.
drawable.ic_account_circle_black_2. 4dp)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.
DEFAULT. _SOUND);
mBuilder.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager)
context.getSystemService(context.
NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
}
button
btnStart.setOnClickListener(new.
View.OnClickListener()
{
#Override
public void onClick(View view) {
Long alertTime =
GregorianCalendar.getInstance().
getTimeInMillis()+5*1000;
Intent alertIntent = new.
Intent(AddCandidate.this, AlertReceiver.class);
AlarmManager alarmManager =
(AlarmManager)getSystemService(
Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.
RTC_WAKEUP, alertTime,
PendingIntent.getBroadcast(
AddCandidate.this, 1,alertIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(AddCandidate.this, "ll",
Toast.LENGTH_SHORT).show();
}
});
Manifest
xmlns:android="
http://schemas.android.com/apk/res/android"
package="com.example.ivan.votingsystem">
<uses-permission. android:name="android.
permission.READ_EXTERNAL_STORAGE" />
<uses-permission. android:name="
android.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="
#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="
android.intent.action.MAIN" />
<category android:name="
android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Dashboard" />
<activity android:name=".AddCandidate"/>
<activity android:name=".VoteNow"></activity>
</application>
</manifest>
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 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");
}
}