Categories
discuss

Check if correct Google Play Service available: “Unfortunately application has stopped working”

Application Crashes every time I run it on my phone. Is there something wrong? It says Unfortunately “appname” has stopped working. I have also tried other approaches to checking for googleplay services but it always crashes. I updated my google play services and have a working google map v2 working perfectly. Any solutions to this code? It crashes on my phone running android 4.1.2 and on my AVD.

package com.example.checkgoogleplayproject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class MainActivity extends Activity {

    @Override
    protected void onResume() {
        super.onResume();

        // Getting reference to TextView to show the status
        TextView tvStatus = (TextView)findViewById(R.id.tv_status);

        // Getting status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status==ConnectionResult.SUCCESS)
            tvStatus.setText("Google Play Services are available");
        else{
            tvStatus.setText("Google Play Services are not available");
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Answer

Thanks guys for your responses. I just figured it out from my LogCat. I had to include this in my Android Manifest.

<application>
<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
...

Categories
discuss

Find the longest common prefix of two strings

I want to find the longest common prefix of two strings. Is there a way to loop my last couple of if statements so that I can end at the last characters that do not match each other?

System.out.println("Enter the first string: ");
String s = input.nextLine();

System.out.println("Enter the second string: ");
String s2 = input.nextLine();

//check if first characters are same
if (s.charAt(0) != s2.charAt(0)) {
  System.out.println(""+s+ " and "+s2+ " have no common prefix");
  System.exit(0);
    }

if (s.charAt(0) == s2.charAt(0))
  System.out.print(" "+s.charAt(0));

if (s.charAt(0) == s2.charAt(0))
  System.out.print(" "+s.charAt(1));

if (s.charAt(0) == s2.charAt(0))
  System.out.print(" "+s.charAt(2));  
  }
}

Example:

Enter first string: Welcome to c++

Enter second string: Welcome to java

The code should return Welcome to as the common prefix.

Answer

try this. I guess this is what you are trying to achieve. If this is correct I will add explanation later

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String s = "Hello Wo";
        String s2 = "Hello World";
        String small,large;
         if(s.length() > s2.length()) 
            {small = s2;large = s;}
          else
            {small = s;large = s2;}
        int index = 0;    
        for(char c: large.toCharArray())
        {
            if(index==small.length()) break;
            if(c != small.charAt(index)) break;
            index++;
        }
        if(index==0)
          System.out.println(""+s+ " and "+s2+ " have no common prefix");
        else
          System.out.println(large.substring(0,index));
    }
}

Edit:

  1. I find the larger of the strings and choose it to be the outer string to loop throught
  2. toCharArray() converts the string into characters so you can loop through each characters in the string using Java’s foreach (For more click[1])
  3. Inside the loop you should exit on two conditions
    • End of the string (I use length to find if I reach end of smaller string)
    • no more matching characters between two strings
  4. you increment the index until you break out in one of the above conditions
  5. By the time you break out of the for loop, index will contain the last index where both string are continuously equal.
  6. If index = 0. just say no matches else print characters from 0 until index
Categories
discuss

How to loop sound in JavaScript?

I tried below code to play a sound in JavaScript for certain times but with no success, the sound plays just once, what is the problem?

for(var i = 0; i < errors; i++){
    PlaySound3();
}

Function:

function PlaySound3() {
    var audioElement = document.getElementById('beep');
    audioElement.setAttribute("preload", "auto");
    audioElement.autobuffer = true;    
    audioElement.load();
    audioElement.play();
};

HTML code:

<audio id="beep">
    <source src="assets/sound/beep.wav" type="audio/wav" />
</audio>

Answer

If you want to play the sound infinitely use the attribute loop in the tag audio :

<audio id="beep" loop>
   <source src="assets/sound/beep.wav" type="audio/wav" />
</audio>

Edit

If you want to stop the loop after 3 times, add an event listener :

HTML:

<audio id="beep">
   <source src="assets/sound/beep.wav" type="audio/wav" />
</audio>

JS:

var count = 1
document.getElementById('beep').addEventListener('ended', function(){
   this.currentTime = 0;
   if(count <= 3){
      this.play();
   }
   count++;
}, false);
Categories
discuss

java double free or corruption

maybe stupid question but i’m getting randomly following error when running my *.jar package. Sometimes programm runs flawlessly and sometimes interrupts putting this out in console:

*** Error in 'java': double free or corruption (out): 0x00007f665c04a830 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7f66c63d7a46]
/usr/lib/libjnetpcap.so(Java_org_jnetpcap_nio_JMemoryReference_disposeNative0+0xa6)[0x7f66b55f20eb]
[0x7f66bc5cd738]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:02 2752983                            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
00600000-00601000 r--p 00000000 08:02 2752983                            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
00601000-00602000 rw-p 00001000 08:02 2752983                            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
006d2000-006f3000 rw-p 00000000 00:00 0                                  [heap]
77b000000-77c500000 rw-p 00000000 00:00 0 
77c500000-785600000 rw-p 00000000 00:00 0 
785600000-78a780000 rw-p 00000000 00:00 0 
78a780000-7d7200000 rw-p 00000000 00:00 0 
7d7200000-7d9b00000 rw-p 00000000 00:00 0 
7d9b00000-800000000 rw-p 00000000 00:00 0 
7f664c000000-7f664c021000 rw-p 00000000 00:00 0 
7f664c021000-7f6650000000 ---p 00000000 00:00 0 
7f6654000000-7f6654021000 rw-p 00000000 00:00 0 
7f6654021000-7f6658000000 ---p 00000000 00:00 0 
7f6658000000-7f6658021000 rw-p 00000000 00:00 0 
7f6658021000-7f665c000000 ---p 00000000 00:00 0 
7f665c000000-7f665c0ac000 rw-p 00000000 00:00 0 
7f665c0ac000-7f6660000000 ---p 00000000 00:00 0 
7f6660000000-7f6660021000 rw-p 00000000 00:00 0 
7f6660021000-7f6664000000 ---p 00000000 00:00 0 
7f6664000000-7f66641ad000 rw-p 00000000 00:00 0 
7f66641ad000-7f6668000000 ---p 00000000 00:00 0 
7f6668000000-7f6668021000 rw-p 00000000 00:00 0 
7f6668021000-7f666c000000 ---p 00000000 00:00 0 
7f666c000000-7f666c021000 rw-p 00000000 00:00 0 
7f666c021000-7f6670000000 ---p 00000000 00:00 0 
7f6670000000-7f6670021000 rw-p 00000000 00:00 0 
7f6670021000-7f6674000000 ---p 00000000 00:00 0 
7f6674000000-7f6674021000 rw-p 00000000 00:00 0 
7f6674021000-7f6678000000 ---p 00000000 00:00 0 
7f667969a000-7f667969d000 ---p 00000000 00:00 0 
7f667969d000-7f667979b000 rw-p 00000000 00:00 0                          [stack:5820]
7f667979b000-7f667979e000 ---p 00000000 00:00 0 
7f667979e000-7f667989c000 rw-p 00000000 00:00 0                          [stack:5819]
7f667989c000-7f667989f000 ---p 00000000 00:00 0 
7f667989f000-7f667999d000 rw-p 00000000 00:00 0                          [stack:5818]
7f667999d000-7f6679bbd000 rw-s 00000000 00:07 220407                     socket:[220407]
7f6679bbd000-7f6679bc0000 ---p 00000000 00:00 0 
7f6679bc0000-7f6679cbe000 rw-p 00000000 00:00 0                          [stack:5817]
7f6679cbe000-7f6679cc1000 ---p 00000000 00:00 0 
7f6679cc1000-7f6679dbf000 rw-p 00000000 00:00 0                          [stack:5816]
7f6679dbf000-7f6679fdf000 rw-s 00000000 00:07 219343                     socket:[219343]
7f6679fdf000-7f6679fe2000 ---p 00000000 00:00 0 
7f6679fe2000-7f667a0e0000 rw-p 00000000 00:00 0                          [stack:5814]
7f667a0e0000-7f667a0fb000 r--s 00000000 08:02 1970077                    /usr/share/fonts/type1/gsfonts/p052003l.pfb
7f667a0fb000-7f667a10d000 r--s 00000000 08:02 1970041                    /usr/share/fonts/type1/gsfonts/n019043l.pfb
7f667a10d000-7f667a126000 r--s 00000000 08:02 1970000                    /usr/share/fonts/type1/gsfonts/b018012l.pfb
7f667a12f000-7f667a130000 rw-p 00000000 00:00 0 
7f667a130000-7f667a133000 rw-s 00000000 00:04 17236023                   /SYSV00000000 (deleted)
7f667a133000-7f667a143000 r--s 00000000 08:02 1969994                    /usr/share/fonts/type1/gsfonts/a010033l.pfb
7f667a143000-7f667a14f000 r--s 00000000 08:02 1970025                    /usr/share/fonts/type1/gsfonts/d050000l.pfb
7f667a14f000-7f667a164000 r--s 00000000 08:02 1970006                    /usr/share/fonts/type1/gsfonts/b018032l.pfb
7f667a164000-7f667a177000 r--s 00000000 08:02 1970003                    /usr/share/fonts/type1/gsfonts/b018015l.pfb
7f667a177000-7f667a191000 r--s 00000000 08:02 1970012                    /usr/share/fonts/type1/gsfonts/c059013l.pfb
7f667a191000-7f667a1a8000 r--s 00000000 08:02 1970094                    /usr/share/fonts/type1/gsfonts/z003034l.pfb
7f667a1a8000-7f667a1c3000 r--s 00000000 08:02 1970080                    /usr/share/fonts/type1/gsfonts/p052004l.pfb
7f667a1c3000-7f667a1d4000 r--s 00000000 08:02 1970047                    /usr/share/fonts/type1/gsfonts/n019063l.pfb
7f667a1d4000-7f667a1e5000 r--s 00000000 08:02 1969997                    /usr/share/fonts/type1/gsfonts/a010035l.pfb
7f667a1e5000-7f667a1ee000 r--s 00000000 08:02 1970089                    /usr/share/fonts/type1/gsfonts/s050000l.pfb
7f667a1ee000-7f667a1fe000 r--s 00000000 08:02 1969991                    /usr/share/fonts/type1/gsfonts/a010015l.pfb
7f667a1fe000-7f667a21a000 r--s 00000000 08:02 1970086                    /usr/share/fonts/type1/gsfonts/p052024l.pfb
7f667a21a000-7f667a232000 r--s 00000000 08:02 1970022                    /usr/share/fonts/type1/gsfonts/c059036l.pfb
7f667a232000-7f667a24d000 r--s 00000000 08:02 1970083                    /usr/share/fonts/type1/gsfonts/p052023l.pfb
7f667a24d000-7f667a25f000 r--s 00000000 08:02 1970009                    /usr/share/fonts/type1/gsfonts/b018035l.pfb
7f667a25f000-7f667a26f000 r--s 00000000 08:02 1969988                    /usr/share/fonts/type1/gsfonts/a010013l.pfb
7f667a26f000-7f667a28a000 r--s 00000000 08:02 1970015                    /usr/share/fonts/type1/gsfonts/c059016l.pfb
7f667a28a000-7f667a29c000 r--s 00000000 08:02 1970044                    /usr/share/fonts/type1/gsfonts/n019044l.pfb
7f667a29c000-7f667a2af000 r--s 00000000 08:02 1970050                    /usr/share/fonts/type1/gsfonts/n019064l.pfb
7f667a2af000-7f667a2c7000 r--s 00000000 08:02 1970018                    /usr/share/fonts/type1/gsfonts/c059033l.pfb
7f667a2c7000-7f667a2e5000 r--s 00000000 08:02 1275703                    /usr/share/texmf/fonts/type1/public/lm/lmr10.pfb
7f667a2e5000-7f667a307000 r--s 00000000 08:02 1978245                    /usr/share/texmf/fonts/type1/public/tex-gyre/qplr.pfb
7f667a307000-7f667a327000 r--s 00000000 08:02 1978229                    /usr/share/texmf/fonts/type1/public/tex-gyre/qbkbi.pfb
7f667a327000-7f667a341000 r--s 00000000 08:02 1978241                    /usr/share/texmf/fonts/type1/public/tex-gyre/qagbi.pfb
7f667a341000-7f667a35c000 r--s 00000000 08:02 1275730                    /usr/share/texmf/fonts/type1/public/lm/lmtcsc10.pfb
7f667a35c000-7f667a376000 r--s 00000000 08:02 1978256                    /usr/share/texmf/fonts/type1/public/tex-gyre/qhvbi.pfb
7f667a376000-7f667a37f000 r--s 00000000 08:02 1969318                    /usr/share/fonts/X11/Type1/c0649bt_.pfb
7f667a37f000-7f667a3a1000 r--s 00000000 08:02 1275718                    /usr/share/texmf/fonts/type1/public/lm/lmtlco10.pfb
7f667a3a1000-7f667a3c1000 r--s 00000000 08:02 1978224                    /usr/share/texmf/fonts/type1/public/tex-gyre/qtmbi.pfb
7f667a3c1000-7f667a3de000 r--s 00000000 08:02 1275759                    /usr/share/texmf/fonts/type1/public/lm/lmssqbx8.pfb
7f667a3de000-7f667a3f9000 r--s 00000000 08:02 1275756                    /usr/share/texmf/fonts/type1/public/lm/lmsso10.pfb

I’m using Ubuntu 13.10 64-bit, running:

java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.13.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)

Thanks for your help 🙂

Answer

I know that this is an old thread but just in case someone fails in the same error.

This is a multi-threading issue with a native library you are using.

To cut the long story short, the java loads the native library into memory then it is accessed by multiple threads at the same time, then this error pops up.

A quick workaround for this issue is to mark the method that generating this error as synchronized.


Update after @kit comment.

It is not possible to run such native library in multi-threading environment.

Your options are:

  • modify the native library to make it thread-safe (if you have access to its code).
  • wrap it as an application and run multiple copies as separate processes (because native library is loaded once per process)
  • wrap it as a service and run multiple copies, talking to it using Sockets or an appropriate higher-level RPC mechanism.

Credits: Load the same dynamic library in two threads (both threads in the same JVM)

Categories
discuss

Extra push notification sent to Android device

I have implemented Parse push notifications but there is an issue on the Android side for me. Whenever I send a notification from Parse, all Android devices receive two notifications, one with the text that I have sent and the other with empty text.

Please suggest what might be wrong here

Note : I have also added native Android notifications (GCM notifications) to my project. Is it possible that they are causing this issue?

Thanks.

The AndroidManifest is setup as follows:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


<application
    android:name=".activityApplication"
    android:label="@string/app_name"
    android:icon="@drawable/icon">

    <activity android:name=".activity"
              android:label="@string/app_name"
              android:screenOrientation="landscape"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:configChanges="orientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.google.android.gms.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>


    <!-- gcm -->
    <receiver
        android:name="com.company.activity.GCBroadcastReceiver"
        android:exported="true"
        android:process=":remote"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="com.company.activity" />
        </intent-filter>
    </receiver>

    <service android:name=".GcmIntentService" />
    <service android:name="com.parse.PushService" />
    <receiver android:name="com.parse.ParseBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.company.activity" />
        </intent-filter>
    </receiver>


</application>

And this is my Broadcast Receiver :

public class GCBroadcastReceiver extends BroadcastReceiver
{
    private static final String TAG = "GCBroadcastReceiver";
    Context ctx;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        ctx = context;

        // Local notification.
            Bundle bundle = intent.getExtras();
        String message = bundle.getString("message");
                String notificationID = bundle.getString("notificationID");

        Log.v(TAG,"Notification message : " + message + "With ID : " + notificationID);

        sendNotification(message, notificationID); 
     }

    // Put the GCM message into a notification and post it.
    private void sendNotification(final String message, final String notificationID)
    {

        Intent notificationIntent = new Intent(ctx, Activity.class);
        PendingIntent intent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx);

        mBuilder.setContentIntent(intent);
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
        mBuilder.setWhen(System.currentTimeMillis());
        mBuilder.setSmallIcon(R.drawable.icon);
        mBuilder.setContentTitle(ctx.getString(R.string.app_name));
        mBuilder.setContentText(message);
        mBuilder.setLights(Color.RED, 400, 400);
        mBuilder.setAutoCancel(true);
        //mBuilder.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + ctx.getPackageName() + "/raw/" + sound));
        mBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);

        NotificationManager mNotificationManager = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());
    }
}

Answer

Parse uses GCM, so if you are sending a notification to an application that uses Parse, and in addition you handle the GCM message with native Android code that displays a notification, the notification may be handled twice (one time by your code and one time by Parse), which will cause it to be displayed twice.

As to why one of the notifications has empty text – the code that displays it probably expects the text to be in an extra parameter that wasn’t sent by the server, and therefore there was no text to display.

UPDATE:

Now that I see you code, I can add the following :

You have two broadcast receivers that handle incoming GCM messages – com.parse.GcmBroadcastReceiver and com.company.activity.GCBroadcastReceiver. Both of them attempt to process the notifications sent to your device from Parse (from your comment below, I understand that the problem doesn’t occur when you send your “local” notifications).

I’m assuming that the Parse library registers on its own to GCM. If you are using a different sender ID (API project ID) for the parse registration and for your native GCM registration, you can fix your problem by checking intent.getExtras ().get("from") in your onReceive method. If it doesn’t contain the sender ID of your local notifications, don’t call sendNotification(message, notificationID);, and the second notification with the empty text won’t be shown.

public void onReceive(Context context, Intent intent)
{
    ctx = context;

    // Local notification.
        Bundle bundle = intent.getExtras();
    String message = bundle.getString("message");
            String notificationID = bundle.getString("notificationID");

    Log.v(TAG,"Notification message : " + message + "With ID : " + notificationID);

    if (intent.getExtras ().get("from").equals (YOUR_SENDER_ID_FOR_LOCAL_NOTIFICATIONS))
        sendNotification(message, notificationID); 
 }

Now, if you are using the same sender ID for both types of notifications, you can either start using different sender IDs, or you can use a different condition – check if intent.getExtras ().get("message") has a value. Your local notifications require this parameter, and I’m assuming the Parse notifications don’t (which explains the notifications without text you are seeing).

Using two GCM broadcast receivers may cause other problems too, so I suggest you look at this question for further reading.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..