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>
Related
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>
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 have a class called ShowBoardList where I check if user has logged in. If user hasn't logged in, then I want to return to the MainActivity which provides the user with buttons to login into different services.
My AndroidManifests.xml looks like this:
<application
<activity android:name="im.chaitanya.TaskTimer.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="im.chaitanya.TaskTimer.WebViewActivity" >
</activity>
<activity android:name="im.chaitanya.TaskTimer.ShowBoardList"
android:label="Your Tasks">
</activity>
</application>
ShowBoardList.java looks like this:
...
Intent mainActivityIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
Intent intent = getIntent();
String url = intent.getStringExtra(WebViewActivity.EXTRA_MESSAGE); //url can be null here
Keys keys = new Keys(); //this gets an API key
SharedPreferences settings = getSharedPreferences("mySettings", 0);
String savedToken = settings.getString("token", "Empty");
if (MyUtils.equalsWithNulls(url,"tasktimer://oauthresponse#token=")) {
Log.d("From ME:", "I've reached inside url check");
mainActivityIntent.putExtra(caller, "ShowBoardList");
//caller is a String. I'm storing the name of the current activity (ShowBoardList) in it.
//So that the main activity (which I'm trying to call) will know where the call came from.
startActivity(mainActivityIntent);
}
if(savedToken.equals("Empty") || savedToken.equals("")) {
String searchString = "#token=";
int tokenIndex = url.indexOf(searchString) + searchString.length(); //Since url can be null there can be an error here
String token = url.substring(tokenIndex);
savedToken = token;
SharedPreferences.Editor editor = settings.edit();
editor.putString("token", token);
editor.apply();
}
...
Condtion equalsWithNulls checks if url is null OR equal to the string in the argument. I have log statements there to check whether control reaches inside the if statement. The main activity however doesn't start.
Edit: onCreate() of MainActivity.java looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("mySettings", 0);
String token = settings.getString("token", "Empty");
Intent intent = new Intent(this, ShowBoardList.class);
if(token != "Empty") {
startActivity(intent);
}
intent = getIntent();
String callerActivity = intent.getStringExtra(ShowBoardList.caller);
View coordinatorLayoutView = findViewById(R.id.snackbarPosition);
if (callerActivity!=null && callerActivity == "ShowBoardList") {
Snackbar
.make(coordinatorLayoutView, "Permission Denied", Snackbar.LENGTH_LONG)
.show();
}
setContentView(R.layout.activity_main);
}
Try to define your new Intent wherever you required.
Intent newIntent = new Intent(ShowBoardList.this, im.chaitanya.TaskTimer.MainActivity.class);
newIntent .putExtra(caller, "ShowBoardList");
startActivity(newIntent );
My solution is based on Sourabh's comment on the question. I realised from my logs that the activity was indeed being started.
What I didn't realise was that when startActivity() is called, the calling activity (in this case ShowBoardList) is paused and when ShowBoardList was being called again, it would resume from after startActivity().
Therefore the solution here was to call finish() and then return immediately after the startActivity() which ensures that onCreate is called the next time. I hope that makes sense if anyone is in the same situation.
These questions helped me understand more about finish():
about finish() in android
onCreate flow continues after finish()
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
For my news-reader app, I made a widget. Using the widget it is possible to start the news-app. But if the user goes back to the home screen, with the back button, there still is an instance of the application. So if the user goes the applications-list (all the app's) and start the news-app again. There are 2 instances of the news app :S For closing the app, the users needs to push 2 times on the back button (because you see 2 times the same app). Can I do anything about it?
Here my code:
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent configIntent = new Intent(context, Main.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setOnClickPendingIntent(R.id.headlinesBox, configPendingIntent);
// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
It seems that you have to add to the manifest:
<activity android:name=".Main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
You can set true on android:clearTaskOnLaunch activity attribute in the Manifest file.
It should always close all the extra Activities and start the App from the first Activity.