What's wrong with this code?

Hello All,

I’m trying to add contact to android phone using uno code, but its not working and build process failed.

android.app.Activity ctx = android.app.Activity.GetAppActivity();
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
// Sets the MIME type to match the Contacts Provider
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

intent.putExtra(ContactsContract.Intents.Insert.NAME, "JonDoe");
intent.putExtra(ContactsContract.Intents.Insert.PHONE, "345523423");
ctx.startActivity(intent);

and here is the include line:

[ForeignInclude(Language.Java, "android.content.Intent", "android.provider.ContactsContract", "android.app.Activity")]

What I would ask is how to to call startActivity on uno file?

Thanks

but its not working and build process failed

What errors are you getting? (especially when building in verbose mode, i.e. fuse build -v -tandroid)

Hi Remi,

Thanks for your respond, here the the full code[updated] for uno file

using Uno;
using Uno.Collections;
using Fuse;
using Fuse.Scripting;
using Fuse.Reactive;
using Uno.Compiler.ExportTargetInterop;



[ForeignInclude(Language.Java, "android.content.Intent", "android.content.ContentProviderOperation", "android.content.Context","android.provider.ContactsContract","java.util.ArrayList","android.util.Log")]
public class AddContact : NativeModule
{
    public AddContact(){
        AddMember(new NativeFunction("saveContact",(NativeCallback)SaveContact));
    }

    object SaveContact(Context c, object[] args){
        SaveContact(c);
        return null;
    }

    [Foreign(Language.Java)]
    static extern(Android) void SaveContact(Context c)
    @{



        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();




        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
        .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
        .build());
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "300050411")
        .build());
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
        .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Ahmedddd")
        .build());

        try {
            c.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        }catch(Exception ex){
            Log.e("LOGTOXML", "Error inserting contact: " + ex.getMessage());
        }   


    @}
}

and here the error

-compile:
 -compile:
    [javac] Compiling 49 source files to /Users/fayezsaeedqandeeel/Desktop/CNP/build/Android/Debug/CNP/app/src/main/bin/classes
    [javac] warning: [options] source value 1.5 is obsolete and will be removed in a future release
    [javac] warning: [options] target value 1.5 is obsolete and will be removed in a future release
    [javac] warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
    [javac] /Users/fayezsaeedqandeeel/Desktop/CNP/build/Android/Debug/CNP/app/src/main/java/com/foreign/AddContact.java:60: error: cannot find symbol
    [javac]                     c.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    [javac]                      ^
    [javac]   symbol:   method getContentResolver()
    [javac]   location: variable c of type Object
    [javac] Note: /Users/fayezsaeedqandeeel/Desktop/CNP/build/Android/Debug/CNP/app/src/main/java/com/foreign/Fuse/Android/StatusBarConfig.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] 1 error
    [javac] 3 warnings

I’ve updated the code above, thank you.

The problem is that you’re trying to use the wrong (type of) context.

The context you’re passing down is the JavaScript context from which the function was called, but in order to call getContentResolver() from your foreign code you need the Android context which you can get from com.fuse.Activity.getRootActivity()

Thanks Remi, works great.