Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? #2

Closed
rubinnellikunnathu opened this issue Apr 20, 2016 · 10 comments

Comments

@rubinnellikunnathu
Copy link

My logcat shows

Process: com.root5solutions.myapplication, PID: 12204 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.root5solutions.myapplication/com.root5solutions.myapplication.MainActivity}: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ContextImpl.startActivity(ContextImpl.java:672) at android.app.ContextImpl.startActivity(ContextImpl.java:659) at android.content.ContextWrapper.startActivity(ContextWrapper.java:331) at com.vistrav.ask.Ask.go(Ask.java:56) at com.root5solutions.myapplication.MainActivity.onCreate(MainActivity.java:104) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

i tried to access permissions such as READ_CONTACT and GET_ACCOUNTSby using the code below

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.vistrav.ask.Ask;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "PM";
    ArrayList<String> alName = new ArrayList<String>();
    ArrayList<String> alContacts = new ArrayList<String>();
    ArrayList<String> alEmails = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Ask.on(getApplicationContext())
                .forPermissions(Manifest.permission.GET_ACCOUNTS
                        , Manifest.permission.READ_CONTACTS) //one or more permissions
                .withRationales("Account permission need for app to work properly",
                        "Contact permission need for app to work properly") //optional
                .when(new Ask.Permission() {
                    @Override
                    public void granted(List<String> permissions) {
                        try {
                            Account[] accounts = AccountManager.get(getApplicationContext()).getAccountsByType("com.google");
                            for (Account account : accounts) {
                                String emailid = account.name;
                                Log.e("account", emailid);
                            }
                        } catch (Exception e) {
                            Log.e("Exception", "Exception:" + e);
                        }
                        ContentResolver cr = getApplicationContext().getContentResolver(); //Activity/Application android.content.Context
                        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
                        if (cursor.moveToFirst()) {
                            do {
                                String contactid = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                                if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{contactid}, null);
                                    while (pCur.moveToNext()) {
                                        String contactName = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                                        String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                        alContacts.add(contactNumber);
                                        alName.add(contactName);
                                        Log.e("Contact Name", contactName);
                                        Log.e("Contact Number", contactNumber);
                                        break;
                                    }
                                    pCur.close();
                                }

                            } while (cursor.moveToNext());
                        }
                        String emailIdOfContact = null;
                        cr = getApplicationContext().getContentResolver();
                        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                                null, null, null);
                        if (cur.getCount() > 0) {
                            while (cur.moveToNext()) {
                                String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));

                                cr.query(
                                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?", new String[]{id}, null);

                                Cursor emails = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id, null, null);
                                while (emails.moveToNext()) {
                                    emailIdOfContact = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                                    alEmails.add(emailIdOfContact);
                                    Log.e("Email ID", emailIdOfContact);
                                }
                                emails.close();

                            }
                        }// end of contact name cursor
                        cur.close();
                        Log.e("ContactL Name", String.valueOf(alName));
                        Log.e("ContactL Number", String.valueOf(alContacts));
                        Log.e("ContactL Email", String.valueOf(alEmails));

                        Log.i(TAG, "granted :: " + permissions);
                    }

                    @Override
                    public void denied(List<String> permissions) {
                        Log.i(TAG, "denied :: " + permissions);
                    }
                }).go();
    }
}

@00ec454
Copy link
Owner

00ec454 commented Apr 20, 2016

here Ask.on(getApplicationContext()) pass activity reference Ask.on(this)

@rubinnellikunnathu
Copy link
Author

but when i deny any permission, app crashes with security exception. did i missed anything.?

@00ec454
Copy link
Owner

00ec454 commented Apr 20, 2016

can you post the stacktrace?

@00ec454
Copy link
Owner

00ec454 commented Apr 20, 2016

btw, did you add the requested permissions in manifest file?

@rubinnellikunnathu
Copy link
Author

rubinnellikunnathu commented Apr 20, 2016

                                                                                  java.lang.RuntimeException: Unable to start receiver com.vistrav.ask.Ask$Receiver: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{c565248 17981:com.root5solutions.myapplication/u0a61} (pid=17981, uid=10061) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                                      at android.app.ActivityThread.handleReceiver(ActivityThread.java:2732)
                                                                                      at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                   Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{c565248 17981:com.root5solutions.myapplication/u0a61} (pid=17981, uid=10061) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
                                                                                      at android.os.Parcel.readException(Parcel.java:1599)
                                                                                      at android.os.Parcel.readException(Parcel.java:1552)
                                                                                      at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
                                                                                      at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
                                                                                      at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
                                                                                      at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
                                                                                      at android.content.ContentResolver.query(ContentResolver.java:475)
                                                                                      at android.content.ContentResolver.query(ContentResolver.java:434)
                                                                                      at com.root5solutions.myapplication.MainActivity$1.granted(MainActivity.java:48)
                                                                                      at com.vistrav.ask.Ask$Receiver.onReceive(Ask.java:87)
                                                                                      at android.app.ActivityThread.handleReceiver(ActivityThread.java:2725)
                                                                                      at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

@rubinnellikunnathu
Copy link
Author

My manifest

`

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET" />

<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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

`

@00ec454
Copy link
Owner

00ec454 commented Apr 20, 2016

you need to add these requested permission into your AndroidManifest.xml file check here for examle

@rubinnellikunnathu
Copy link
Author

no its not the problem. i think the problem is whenever i press deny permission button, it redirects to method void granted. there the code works without permission and finally endup in security exception (because permission is not given yet,but still it goes to granted method)

@00ec454
Copy link
Owner

00ec454 commented Apr 20, 2016

I know why
the method public void granted(List<String> permissions) will be called even if permission is denied but the list of permissions will be empty, you need to check what list if the desired permission has been granted by user, if yer the permission list will contain that permission else it will be in public void denied(List<String> permissions) permission list.
to avoid this error you need to put if condition in public void granted(List<String> permissions) and if particular permission granted then you can go ahead to check your logic. you can do something like

@Override
 public void granted(List<String> permissions) {
   if(permissions.contains(Manifest.permission.GET_ACCOUNTS)){
     // get account granted and put your logic here   
   }
   if(permissions.contains(Manifest.permission.READ_CONTACTS)){
     // CONTACTS granted and put your logic here   
   }
 }

@rubinnellikunnathu
Copy link
Author

It worked . nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants