I would like to make an app that starts up every time the device is unlocked.
I'm new to Android and although I've read dozens of answers and the documentation there are just too many moving parts that I'm having a hard time troubleshooting.
Here is what I have ...
Structure
app
- manifests
-- AndroidManifest.xml
- java
-- DisplayMessageActivity.java
-- MainActivity.java
- res
- UnlockReceiver.java
This is in in my AndroidManifest.xml
<receiver android:name="UnlockReceiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
This is my UnlockReceiver.java class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.xywebsolutions.myapplication.MainActivity;
public class UnlockReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
What am I doing wrong? Is it my Structure? Do I need to add permissions before adding the receiver?
You don't need any broadcast to start/stop video.
You can use onResume() of activity lifecycle to start video. onResume will be called when activity becomes visible to user.
onPause() of activity to pause the video. You can also save current play position and start from that position when activity started again using onSaveInstanceState() function
For your case you can try this
#Override
public void onReceive(Context context, Intent intent) {
//start activity
Intent i = new Intent();
i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
two changes and this issue was fixed:
1) The "UnlockReceiver" was moved into the java folder
app
- manifests
-- AndroidManifest.xml
- java
-- DisplayMessageActivity.java
-- MainActivity.java
-- UnlockReceiver.java
- res
And
2) The receiver in the manifest needed android:exported="true" ...
<receiver android:name="UnlockReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
Related
i`am new to unit testing and i have this task to write unit testing for Post API i have only the end point and api body i read a lot in google but i could not figure out how to start , any help ?
You must create a intent, fill it with data if needed, like the data extracted from the QR code and then call startActivity.
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();
I put an int in the bundle but it could be anything that implements Serializable.
You can reach direct on required screen with that pages’s activity (as shown Splash screen in below example). You can launch activity with below method.
public static void launchActivity(Activity activityName)
{
((AndroidDriver<MobileElement>) driver).startActivity(activityName);
}
How you can call this function
Suppose you have below app package and activity (its for example but you have to use for your app)
String appPackage ="my.app.helloworld";
String appActivity = "my.app.helloworld".common.activity.SplashScreen";
launchActivity(new Activity(appPackage, appActivity));
You need to set android:exported="true" in your AndroidManifest.xml file to resolve error java.lang.SecurityException
<activity
android:name="com.dsquares.lucky/.screens.mainscreens.Wallet.WalletPayment.AddFundsActivity"
android:label="wallet"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" >
</action>
</intent-filter>
</activity>
I'm new to Android development, and I'm trying to create a simple "proof of concept application" which will run as a background service. I'm trying to use IntentService with a BroadcastReceiver to kick off the process (during boot time for now, at some point I might switch it to Screen on / user present).
I created a new project within Android Studio with no activity. Then I added the following Java files and made the following changes to the AndroidManifest.xml.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.circlesquires.netcountable.netcountable">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name=".SnapshotService" android:exported="true">
</service>
<receiver android:name=".ServiceStarter" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
ServiceStarter.java
package com.circlesquires.netcountable.netcountable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by camha on 6/18/2016.
*/
public class ServiceStarter extends BroadcastReceiver{
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("output", "onReceive occured!");
if(intent.getAction().equals(ACTION)) {
Intent serviceIntent = new Intent(context, SnapshotService.class);
context.startService(serviceIntent);
}
}
}
SnapshotService.java
package com.circlesquires.netcountable.netcountable;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
/**
* Created by camha on 6/18/2016.
*/
public class SnapshotService extends IntentService {
public SnapshotService() {
super("SnapshotService");
}
#Override
protected void onHandleIntent(Intent workIntent) {
while(true) {
Log.i("output", "I'm running!");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
I deploy the application to an emulator being sure to click the "debug" button. However I don't ever see ANY of the logs outputted in logcat.
I'm sure I'm doing something wrong--help me figure out what :D. Thanks!
android.intent.action.BOOT_COMPLETED is send, as its name suggest, after, but only after a device boot time. Are you restarting your device?
Instead of restarting device to test out your application, you can send system broadcasts directly through adb:
./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n <your.package.name>/.<YourReceiverClass>
You can find more info here.
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, SnapshotService.class);
context.startService(i);
}
compare direct with intent value instead of making string
and one thing more
On Android 3.1 and higher, the user must launch one of your activities before any manifest-registered BroadcastReceiver will work.
check this https://stackoverflow.com/a/17067671/3288890
i have an app and it's important for me to detect time changes. so far i have receiver with proper intent filters like this:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.ACTION_TIME_CHANGED" />
<action android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
and this is my receiver :
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
SharedPreferences.Editor spe =PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit();
spe.putLong("UFCount", 1));
spe.putLong("FCount", 1));
spe.commit();
Toast.makeText(context.getApplicationContext(), "Your job is being done!!", Toast.LENGTH_LONG).show();
}
}
this receiver only works when the app is open\in-the-background . how can i make this receiver to work even if the app is closed?
Since the application is closed, there is no UI (i.e. no Activity) to display the Toast. At this point, there should be an exception in Logcat. You might also add a logcat trace before displaying the Toast, like Log.d("MyReceiver", "in onCreate()"); and you should see it even when the app is closed.
Instead of displaying a Toast, you could for instance send a local notification, which is allowed because it does not require an Activity from your app.
I'm going by the following code example to dynamically build a preference activity.
http://www.linuxtopia.org/online_books/android/devguide/guide/samples/ApiDemos/src/com/example/android/apis/app/PreferencesFromCode.html
The preference dialog shows, but I'm not able to see any changes after closing it.
Here's where I'm defining the activity in AndroidManifest.xml
<activity
android:name="PreferencesActivity" android:label="#string/preferences_name">
</activity>
Here's where I'm defining the receiver.
<receiver
android:name="FroyVisualReceiver"
android:label="#string/app_name"
android:exported="false">
<intent-filter>
<action android:name="com.starlon.froyvisuals.PREFS_UPDATE"/>
</intent-filter>
</receiver>
And here's the BroadcastReceiver. I never see the "WTF" in logcat. What am I doing wrong?
package com.starlon.froyvisuals;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
public class FroyVisualsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("WTFWTF", "WTFWTFWTFW");
String action = intent.getAction();
if(action.equals("com.starlon.froyvisuals.PREFS_UPDATE"))
{
((FroyVisuals)context).updatePrefs();
}
}
}
Oh here's onPause where I'm broadcasting the PREFS_UPDATE intent.I do see the logcat message. This method is part of my PreferenceActivity.
/** another activity comes over this activity */
#Override
public void onPause()
{
Log.i(TAG, "onPause ================================ ");
super.onPause();
Intent i = new Intent(this, FroyVisualsReceiver.class);
i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
sendBroadcast(i);
}
Edit: I think it may have to do with this line. 'this' points to my PreferenceActivity.
Intent i = new Intent(this, FroyVisualsReceiver.class);
Try a simple Intent:
Intent i = new Intent();
i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
sendBroadcast(i);
I am creating a class which uses broadcast receiver. I want to receive the broadcast on unlocking of the phone. But there is some issue. Please help me out.
My Manifest.xml is :-
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
and my Broadcast reciever class :-
public class MyReiever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Though other application and services are receiving broadcast for "Screen_on" and "USer_Present" eg. WifiService.
Although the Java constants are android.content.intent.ACTION_USER_PRESENT, android.content.intent.ACTION_BOOT_COMPLETED, and android.content.intent.ACTION_SCREEN_ON, the values of those constants are android.intent.action.USER_PRESENT, android.intent.action.BOOT_COMPLETED, and android.intent.action.SCREEN_ON. It is those values which need to appear in your manifest.
Note, however, that a receiver for ACTION_SCREEN_ON can not be declared in a manifest but must be registered by Java code, see for example this question.
Since implicit broadcast receivers are not working as of Android 8.0, you must register your receiver by code and also in the manifest.
Do these steps:
Add manifest tag
<receiver android:name=".MyReciever">
<intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</intent-filter>
</receiver>
Create a receiver class and add your codes
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("My Reciever","is intent null => " + (intent == null));
Log.d("My Reciever",intent.getAction()+"");
}
}
Create a service and register the receiver in it
public class MyService extends Service {
MyReceiver receiver = new MyReceiver();
#Override
public IBinder onBind(Intent intent) { return null; }
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(receiver);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
Don't forget to define the service in manifest
<service android:name=".MyService"/>
To make your broadcast work, you have to register it in service. And to keep your service alive you can use tools like alarm managers, jobs, etc which is not related to this question.
Check your Class name, that is extending BroadcastReceiver. It should be "MyReciever" not "MyReiever"
Beside Typo that mentioned in earlier answers and the fact that the receiver class package name should be completely mentioned in receiver tag, I think the problem is that the code in question uses two nested intent filters. I believe this code will work correctly:
<receiver android:name=".MyReciever">
<intent-filter>
<action android:name="android.intent.action.ACTION_USER_PRESENT" />
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SCREEN_ON" />
</intent-filter>
</receiver>
I can only give you a quick tip as i have gone through that path you are following with much less success. Try to read logcat using java.util.logging so that you will not require permission to read logs. And in log view create listener for the one containing "system disable" as its header. it fires up both at lock and unlock. check for the one that gives access to system.android not the other screen.
Hope it helps. Best of luck