Log.d seems to not be displaying anything in my terminal. Is this related to using a static method?
public static void setPosition(int pos){
DetailActivity.pos = pos;
DetailActivity.counter++;
Log.d("counter", "" + counter);
if(DetailActivity.counter == 1){
//ad
if (DetailActivity.mInterstitialAd.isLoaded()) {
DetailActivity.mInterstitialAd.show();
counter = 0;
}
}
}
Log is going to print to LogCat. In Android Studio, go to the Android Monitor tab to see this output.
You may add filters for your tag or message in the search box, for example entering "counter" should show your message.
Related
Last week I started learning Android as I needed to create an application for one of the projects at Uni.
The application is a simple barcode/QRcode scanner and it should scan the code, compare its result with the database (I'm using Firebase) and either return other data from database if the barcode is found or ask the user if he wants to add the barcode to the database if it's not found.
I thought the easiest way to do it would be to use AlertDialog, but the app crashes every single time I scan the code.
I debugged the app and checked the Logcat, what I get is:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
This is exactly where I get the error and where I wanted to use AlertDialog - based on the value in the variable details.
private BarcodeCallback callback = new BarcodeCallback() {
#Override
public void barcodeResult(final BarcodeResult result) {
barcodeView.decodeSingle(callback);
dbRef.child("Items").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot> item = dataSnapshot.getChildren().iterator();
Boolean isFound = false;
while (!isFound || item == null) {
DataSnapshot i = item.next();
String check = i.child("ID").getValue().toString();
if (result.getText().equals(check)) {
isFound = true;
details = "Consumption: " + i.child("Consumption").getValue().toString()
+ "\nCost: " + i.child("Cost").getValue().toString()
+ "\nName: " + i.child("Name").getValue().toString();
} else {
details = "Not found";
}
}
new AlertDialog.Builder(getApplicationContext())
.setMessage("This is just an example for the purpose of the question.")
.create()
.show();
}
I get the error exactly on the line with .show();.
In the previous posts I found that you can't display AlertDialog in this place, and you need to use runOnUiThread function or Handler, none of those options worked for me, and I was getting the error in the same place.
Do you guys have any advice or suggestions?
Also, I'm sorry for the way this post looks like or for any missing but required information. I know it's not an excuse, but this is my first post here.
Thanks in advance for any replies.
The problem is here:
new AlertDialog.Builder(getApplicationContext())
You can't build a Dialog using the application context. To reach this you need an Activity Context.
Read this question or this article for further understanding
Right now i'm doing an educational app.I stucked with some problem.
My app first page has list of java programs name like below
swap two numbers
find biggest number
reverse number
When user press a particular item- it has to go to detail screen and has to display particular program (i mean user press on swap of two no's need to navigate to detail page to display respective program)->right now its going detail view controller
For example I have program like this
/********print star pattern ************/
import java.util.Scanner;
public class Diamond
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter N : ");
int n=sc.nextInt();
System.out.print("Enter Symbol : ");
char c = sc.next().charAt(0);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++)
{
System.out.print(c);
}
System.out.println();
}
for(int i=n-1;i>0;i--)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i*2-1;j++)
{
System.out.print(c);
}
System.out.println();
}
}
}
How can I print above program in detail in view controller ?
Now am using textview, Is it okay to continue with it or Is any other component that i could make use of ?because some programs have more width and height.
or else any other mechanism is available to achieve this?
I researched a lot but didn't find solution.
Can someone suggest me how to go with this? where to store the program? How to display it?
Thanks.
Let's say I have a similar scenario:
I'm using an accessibility service in order to my TTS engine talk when this dialog appears, but the only thing I was able to detect were the selectable views (those pointed by the arrow).
Is there any way to detect the title and (more importantly) whole text inside the dialog?
Yes. I think it is likely that you're grabbing these items off of accessibility events, which focus on a single node. What you want to do instead is look at the entire view hierarchy. You can do this one of two ways. First thing to note is that Accessibility Nodes are a tree. Just like the view heirarchy is a tree. In fact, this tree matches the view hierarchy, almost 1 to 1. Developers can force an element to not be included in the view hierarchy, though this isn't done often in practice. Even if they do you can get this information regardless. Let's assume we want this information. First thing we want to do is make sure it's included.
protected void onServiceConnected() {
super.onServiceConnected();
AccessibilityServiceInfo tempInfo = getServiceInfo();
tempInfo.flags |= AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(tempInfo);
}
It's highly likely you can skip this step, but just in case!
After this it's quite simple. First, so that you can see where this information is, let's write a cute little logging function.
public static void logNodeHeirarchy(AccessibilityNodeInfo nodeInfo, int depth) {
if (nodeInfo == null) return;
String logString = "";
for (int i = 0; i < depth; ++i) {
logString += " ";
}
logString += "Text: " + nodeInfo.getText() + " " + " Content-Description: " + nodeInfo.getContentDescription();
Log.v(LOG_TAG, logString);
for (int i = 0; i < nodeInfo.getChildCount(); ++i) {
logNodeHeirarchy(nodeInfo.getChild(i), depth + 1);
}
}
This function should log the entire tree of an accessibility node. Add it as a static function to your accessibility service. Now we just need to call it on the root node. You can easily change the properties that get logged. I find text, content description, and view id to be the most useful.
#Override
public void onAccessibilityEvent(AccessibilityEvent e) {
switch (e.getEventType()) {
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {
logNodeHeirarchy(getRootInActiveWindow(), 0);
}
}
}
This should allow you to see where the information is. All you have to do is figure out how to parse it. Note: You can also crawl up from a leaf node, using getParent().
I have been able to create the if statement that checks for the string and it returns the toast message that i created but it keeps showing the toast message every time i open the chat. even if the most recent message doesn't contain the string I am looking for so i am assume it isn't checking to see if it is the last message received and it doesn't check to see if it is unread. the code is below. the reason i am trying to do this is because my parents share a facebook account and i want an easy way to display if the message is signed mom or dad. the code below only has the check for mom once it works i will be adding the check for dad signature. I am using the open source message client Xabber. Thank you for help.
public void setVisibleChat(String account, String user) {
final boolean remove = !AccountManager.getInstance()
.getArchiveMode(account).saveLocally();
AbstractChat chat = getChat(account, user);
if (chat == null)
chat = createChat(account, user);
else {
// Mark messages as read and them delete from db if necessary.
final ArrayList<MessageItem> messageItems = new ArrayList<MessageItem>();
for (MessageItem messageItem : chat.getMessages()) {
if (!messageItem.isRead()) {
messageItem.markAsRead();
messageItems.add(messageItem);
}
if (chat.getLastText().contains("Mom") && (!messageItem.isRead()));{
Toast.makeText(Application.getInstance(), "Message from Mom!", Toast.LENGTH_SHORT).show();
}
}
Application.getInstance().runInBackground(new Runnable() {
#Override
public void run() {
Collection<Long> ids = getMessageIds(messageItems, remove);
if (remove)
MessageTable.getInstance().removeMessages(ids);
else
MessageTable.getInstance().markAsRead(ids);
}
});
}
visibleChat = chat;
}
You've got an extra semi-colon here
if (chat.getLastText().contains("Mom") && (!messageItem.isRead())); <------
So your next block of code containing the Toast show statement will always be executed.
Remove the semi-colon
I'm trying to track how an app is being installed from start and so far I've been able to trace the source code where the call goes to PackageManagerService which extends the PackageManager and calls the method installPackageWithVerification(), the code for which is shown below for reference:
#Override
public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer, int flags, String installerPackageName, Uri verificationURI, ManifestDigest manifestDigest) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, null);
final int uid = Binder.getCallingUid();
final int filteredFlags;
if (uid == Process.SHELL_UID || uid == 0) {
if (DEBUG_INSTALL) {
Slog.v(TAG, "Install from ADB");
}
filteredFlags = flags | PackageManager.INSTALL_FROM_ADB;
} else {
filteredFlags = flags & ~PackageManager.INSTALL_FROM_ADB;
}
final Message msg = mHandler.obtainMessage(INIT_COPY);
msg.obj = new InstallParams(packageURI, observer, filteredFlags, installerPackageName,
verificationURI, manifestDigest);
mHandler.sendMessage(msg);
}
The last 3 lines of code above create a message handler object and basically encapsulates the package install params in the message and sends it. I'm not clearly sure where the message gets delivered.
I went and debugged the android.os.Handler which basically defines the sendMessage(), all i can find is this message gets added to the a queue natively using the instance of android.os.MessageQueue
Could someone kindly enlighten me on what happens next to the package? or even the message en-queued in the NativeMessageQueue.
Thanks in advance!
I've got it figured out. My bad the PackageHandler is defined somewhere on top in the same file. Its hard to find when you are browsing through file with 3K lines of code :-P