I want to create programm for autochanging network mode. I'm already read same topics and parically found parts of code, but I have little problem.
Boolean success = (Boolean) setPreferredNetwork.invoke(mTelephonyManager,
networkType);
In this part of code, I don't know how to define "mTelephonyManager", can someone help me with this? Almost full code below.
MainActivity
package com.example.a4genforce;
import * *
public class MainActivity extends Activity {
int networkType = 12; //LTE/WCDMA
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setPreferredNetwork(networkType);
}
public Method getHiddenMethod(String methodName, Class fromClass, Class[] params) {
Method method = null;
try {
Class clazz = Class.forName(fromClass.getName());
method = clazz.getMethod(methodName, params);
method.setAccessible(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return method;
}
public void setPreferredNetwork(int networkType) {
try {
Method setPreferredNetwork = getHiddenMethod("setPreferredNetworkType",
TelephonyManager.class, new Class[]{int.class});
Boolean success = (Boolean) setPreferredNetwork.invoke(mTelephonyManager,
networkType);
Log.i(TAG, "Could set Network Type ::: " + (success.booleanValue() ? "YES" : "NO"));
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void onMyClick(View view) {
Toast.makeText(this, "Зачем вы нажали?", Toast.LENGTH_SHORT).show();
try {
Intent intent = new Intent("android.intent.action.MAIN");
intent.setClassName("com.android.settings", "com.android.settings.RadioInfo");
startActivity(intent);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), " Device not supported", Toast.LENGTH_LONG).show();
}
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.a4genforce">
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" tools:ignore="ProtectedPermissions"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Related
Specifically I am looking to upload a file with extension .xls
I have a project that tries to do this, but the following error exists
E/AndroidRuntime: FATAL EXCEPTION: Thread-6
Process: com.example.excelpromodel, PID: 12651
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/methods/HttpPut;
at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:2390)
at com.dropbox.client2.DropboxAPI.putFileOverwriteRequest(DropboxAPI.java:1760)
at com.dropbox.client2.DropboxAPI.putFileOverwrite(DropboxAPI.java:1726)
at com.example.excelpromodel.SQLite2ExcelActivity$4.run(SQLite2ExcelActivity.java:267)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.client.methods.HttpPut" on path: DexPathList[[zip file "/data/app/com.example.excelpromodel-pu6SR7F9dUiyu9bWnHwX8w==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.excelpromodel-pu6SR7F9dUiyu9bWnHwX8w==/lib/arm, /system/lib, /system/vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:2390)
at com.dropbox.client2.DropboxAPI.putFileOverwriteRequest(DropboxAPI.java:1760)
at com.dropbox.client2.DropboxAPI.putFileOverwrite(DropboxAPI.java:1726)
at com.example.excelpromodel.SQLite2ExcelActivity$4.run(SQLite2ExcelActivity.java:267)
at java.lang.Thread.run(Thread.java:764)
My code is:
public class SQLite2ExcelActivity extends AppCompatActivity {
public static Button button1;
public static String path = Environment.getExternalStorageDirectory().getPath() + "/Backup/";
public static File Dir = new File(path);
public void onClickBut1(View v) {
UploadToDropboxFromPath(path + "test.txt", "excel/test.txt");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite_2_xl);
button1 = findViewById(R.id.button1);
initViews();
AndroidAuthSession session = buildSession();
dropboxAPI = new DropboxAPI<AndroidAuthSession>(session);
}
static DropboxAPI<AndroidAuthSession> dropboxAPI;
private static final String APP_KEY = "";
private static final String APP_SECRET = "";
private static final String ACCESSTOKEN = "";
private DropboxAPI.UploadRequest request;
private AndroidAuthSession buildSession()
{
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
session.setOAuth2AccessToken(ACCESSTOKEN);
return session;
}
static final int UploadFromSelectApp = 9501;
static final int UploadFromFilemanager = 9502;
public static String DropboxUploadPathFrom = "";
public static String DropboxUploadName = "";
public static String DropboxDownloadPathFrom = "";
public static String DropboxDownloadPathTo = "";
private void UploadToDropboxFromPath (String uploadPathFrom, String uploadPathTo)
{
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final String uploadPathF = uploadPathFrom;
final String uploadPathT = uploadPathTo;
Thread th = new Thread(new Runnable()
{
public void run()
{
File tmpFile = null;
try
{
tmpFile = new File(uploadPathF);
}
catch (Exception e) {e.printStackTrace();}
FileInputStream fis = null;
try
{
fis = new FileInputStream(tmpFile);
}
catch (FileNotFoundException e) {e.printStackTrace();}
try
{
dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
}
catch (Exception e) {}
getMain().runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == UploadFromFilemanager)
{
final Uri currFileURI = intent.getData();
final String pathFrom = currFileURI.getPath();
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
Thread th = new Thread(new Runnable()
{
public void run()
{
getMain().runOnUiThread(new Runnable()
{
#Override
public void run()
{
UploadToDropboxFromPath(pathFrom, "/db-test/" + DropboxUploadName + pathFrom.substring(pathFrom.lastIndexOf('.')));
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}...
}
public String getPath(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Audio.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
if(s!=null) {
cursor.close();
return s;
}
}
catch(Exception e){}
try {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
if(s!=null) {
cursor.close();
return s;
}
}
catch(Exception e){}
try {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
cursor.close();
return s;
}
finally {
if (cursor != null) {
cursor.close();
}
}
}
public SQLite2ExcelActivity getMain()
{
return this;
}
AndroidManifest..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.excelpromodel">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask" >
<intent-filter>
<data android:scheme="db-akg125xzg45a9gc" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SQLite2ExcelActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" />
<activity
android:name=".Excel2SQLiteActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme" />
</application>
</manifest>
and dependences
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.ajts.androidmads.SQLite2Excel:library:1.0.4'
implementation 'com.ajts.androidmads.sqliteimpex:library:1.0.0'
implementation files('libs/dropbox-android-sdk-1.6.3.jar')
implementation files('libs/json_simple-1.1.jar')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
get and upload to Dropbox the xls file that is in the "Backup" path with a single button and not from the file manager. Thank you all
code below is used for trying out the file provider in preparation for android Nougat:
manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.securefilesharing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HomeSecureFileShareTestActivity"
android:label="#string/title_activity_home_secure_file_share_test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.securefilesharing.fileprovider"
android:enabled="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_path" />
</provider>
</application>
</manifest>
file provider xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="/"/>
</paths>
constructing URI and passing to camera intent:
private Uri createImageFile() throws Exception {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), System.currentTimeMillis() + ".jpg");
//File file = new File(System.currentTimeMillis() + ".jpg");
Uri photoUri = null;
try{
photoUri = FileProvider.getUriForFile(this, "com.securefilesharing.fileprovider", file);
}
catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
return photoUri;
}
View.OnClickListener ocl = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
uri = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} catch (Exception e) {
Log.e(TAG, TAG + ": " + e.toString());
}
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
};
I also did the activity.requestPermissions(forPermissions.toArray(new String[0]), PERMISSION_CODE); for the camera, read and write external file
below is the exception message:
06-22 15:04:13.962: E/HomeSecureFileShareTestActivity(31851): HomeSecureFileShareTestActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.securefilesharing.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_path" />
</provider>
change your provider part of manifest...
Refer:: https://developer.android.com/guide/topics/manifest/provider-element.html#enabled
Try this,
private Context mContext=YourActivity.this;
private static final int REQUEST = 112;
View.OnClickListener ocl = new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.CAMERA};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
openCamera()
}
} else {
openCamera()'
}
}
};
get Permissions Result
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Toast.makeText(mContext, "The app was not allowed to write in your storage", Toast.LENGTH_LONG).show();
}
}
}
}
check permissions for marshmallow
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
openCamera Funtion
public void openCamera()
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
uri = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} catch (Exception e) {
Log.e(TAG, TAG + ": " + e.toString());
}
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
createImageFile funtion:
public Uri createImageFile() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/TEST");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fName = "Image_" + n + ".jpg";
File file = new File(myDir, fName);
if (file.exists()) {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri uriImage = Uri.fromFile(file);
return uriImage;
}
File file = new File(Environment.getExternalStorageDirectory(), "/download/"+ GlobalVars.apkname+".apk");
System.out.println("file>>>>>"+file);
Uri fileUri = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(MainActivity.this,getPackageName(), file);
System.out.println("File URI>>>>>"+fileUri);
//fileUri = FileProvider.get
}
I'm trying to make a connection with a python server I made but everytime I press the button my app crashes.
Here is my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
String ip = "<IP>";
int port = <PORT>;
try {
Socket s = new Socket(ip, port);
System.out.println("connected!");
s.close();
} catch (IOException e) {
System.out.println(e);
}
}
});
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<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>
I really don't understand what went wrong
You are doing network on main UI thread which is not allowed. So use async Task and do this task in background thread. For that, create a class (for ex. background.java )and use Async task in it.
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
background bg = new background();
bg.execute();
}
});
and in your background class:
public class background extends AsyncTask<Void,Void,Void> {
#Override
protected Void doInBackground(Void... params) {
String ip = "<IP>";
int port = <PORT>;
try {
Socket s = new Socket(ip, port);
System.out.println("connected!");
s.close();
} catch (IOException e) {
System.out.println(e);
}
return null;
}
}
Hope this helps :)
Objective: send a ping to android clients. I have an array of device registration Id's held in my user object.
In GCMIntentService.java, auto created within my app, I get that registration id here
/**
* Called back when a registration token has been received from the Google
* Cloud Messaging service.
*
* #param context
* the Context
*/
#Override
public void onRegistered(Context context, String registration) {
Log.d(TAG, "onRegistered(context, registration), Registration: " + registration);
and then create the DeviceInfo object (also pre defined with app engine) and then add this ID to the user through another endpoint. I have confirmed this works/ see the string held and assume that my device is now registered properly.
When certain things happen in the backend, I have a custom notification class and run this method:
public void sendNotificationPingToUsers(
#Named("userIds") ArrayList<Long> userIds,
ZeppaNotification notification) throws IOException {
Sender sender = new Sender(Constants.SENDER_ID);
PersistenceManager mgr = getPersistenceManager();
try {
ArrayList<String> allDevices = new ArrayList<String>();
for (int i = 0; i < userIds.size(); i++) {
long userId = userIds.get(i);
ZeppaUser zeppaUser = mgr
.getObjectById(ZeppaUser.class, userId);
if (zeppaUser != null) {
ZeppaNotification specificNotification = new ZeppaNotification();
specificNotification.setToUserId(userId);
specificNotification.setFromUserId(notification
.getFromUserId());
specificNotification.setEventId(notification.getEventId());
specificNotification.setExtraMessage(notification
.getExtraMessage());
specificNotification.setNotificationType(notification
.getType());
String extraMessage = specificNotification
.getExtraMessage();
if (extraMessage.length() > 1000) {
extraMessage = extraMessage.substring(0, 1000)
+ "[...]";
}
mgr.makePersistent(specificNotification);
allDevices.addAll(zeppaUser.getDevices());
}
}
if (!allDevices.isEmpty()) {
Message msg = new Message.Builder().collapseKey("sendToSync")
.build();
MulticastResult result = sender.send(msg, allDevices, 5);
result.getTotal();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
mgr.close();
}
}
I pass in the other notification so I can recreate it for all users, hold it in the notification table, then ping the device so that it can pull this and any others unseen and create a status bar notification.
This method, again in GCMIntentService:
#Override
public void onMessage(Context context, Intent intent) {
Log.d(TAG, "received message ping");
Is never called and this is where I am trying to handle everything from the device side of things. Can anyone point out what I may be doing wrong or if I am incorrectly interpreting the way this service works?
Thank you
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<permission
android:name="com.minook.zeppa.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.minook.zeppa.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".ZeppaApplication"
android:allowBackup="true"
android:icon="#drawable/zeppa_icon"
android:label="#string/app_name"
android:testOnly="false"
android:theme="#style/ZeppaTheme"
android:uiOptions="none" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:logo="#drawable/zeppa_icon"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CreateAccountActivity"
android:label="#string/create_account"
android:logo="#drawable/zeppa_icon" >
</activity>
<activity
android:name=".NewFriendsActivity"
android:label="#string/add_friends"
android:logo="#drawable/zeppa_icon" >
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".EventViewActivity"
android:label="#string/app_name"
android:logo="#drawable/zeppa_icon" >
</activity>
<activity
android:name=".NewEventActivity"
android:label="#string/app_name"
android:logo="#drawable/zeppa_icon" >
</activity>
<activity
android:name=".UserActivity"
android:label="#string/app_name"
android:logo="#drawable/zeppa_icon" >
</activity>
<activity
android:name=".ZeppaPreferenceActivity"
android:label="#string/event_details"
android:logo="#drawable/zeppa_icon" >
</activity>
<!--
<activity
android:name=".RegisterActivity"
android:launchMode="singleTop" >
</activity>
-->
<service android:name=".GCMIntentService" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.minook.zeppa" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.minook.zeppa" />
</intent-filter>
</receiver>
</application>
Another Note: i have tried using the project number and a server key generated by the GAE console with no specified IP. Neither have worked so far.
I just realized I never came back to this.
Turns out that he app-engine created GCM code isn't the ideal way to do it. This is is the essential code in my application with now successfully sends a send-to-sync from the backend and displays a notification in the status bar.
public class ZeppaGCMReceiver extends WakefulBroadcastReceiver {
final private static String TAG = "GCMIntentService";
private static String registrationId = null;
public static void register(final ZeppaApplication application) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(application
.getApplicationContext());
try {
registrationId = gcm.register(Constants.PROJECT_NUMBER);
Log.d(TAG, "gcm.register( " + registrationId + " )");
ZeppaUser currentUser = ZeppaUserSingleton.getInstance().getUser();
if (currentUser.getDevices() == null
|| !currentUser.getDevices().contains(registrationId)) {
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
Zeppauserendpoint.Builder endpointBuilder = new Zeppauserendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
application.getGoogleAccountCredential());
endpointBuilder = CloudEndpointUtils
.updateBuilder(endpointBuilder);
Zeppauserendpoint userEndpoint = endpointBuilder
.build();
try {
RegisterUserDevice registerTask = userEndpoint
.registerUserDevice(
ZeppaUserSingleton.getInstance().getUserId(),
registrationId);
registerTask.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}.execute();
} else {
Log.d(TAG, "Already Registered");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void unregister(final ZeppaApplication application) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(application
.getApplicationContext());
// TODO: registration id in preferences and
try {
gcm.unregister();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received message ping");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String messageType = gcm.getMessageType(intent);
Log.d(TAG, "MessageType: " + messageType);
if (messageType == null) {
Log.d(TAG, "Message is null");
return;
} else if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
Log.d(TAG, "Error!");
return;
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
Log.d(TAG, "Deleted");
return;
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
Log.d(TAG, "Message");
handlePingInAsync(context);
} else {
Log.d(TAG, "WTF are you..? " + intent.toString());
}
}
private void handlePingInAsync(Context context) {
Context[] param = {context};
new AsyncTask<Context, Void, Void>() {
#Override
protected Void doInBackground(Context... params) {
Context context = params[0];
GoogleAccountCredential credential = getCredential(context);
if (credential == null) {
return null;
}
Zeppanotificationendpoint.Builder endpointBuilder = new Zeppanotificationendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(), credential);
endpointBuilder = CloudEndpointUtils
.updateBuilder(endpointBuilder);
Zeppanotificationendpoint notificationEndpoint = endpointBuilder
.build();
try {
SharedPreferences prefs = context.getSharedPreferences(
Constants.SHARED_PREFS, Context.MODE_PRIVATE);
Long userId = prefs.getLong(Constants.USER_ID, -1);
if (userId > 0) {
GetUnseenNotifications getUnseenNotifications = notificationEndpoint
.getUnseenNotifications(userId);
CollectionResponseZeppaNotification collectionResponse = getUnseenNotifications
.execute();
if (collectionResponse == null
|| collectionResponse.getItems() == null) {
} else {
List<ZeppaNotification> notifications = collectionResponse
.getItems();
sendNotificationsForResult(notifications,
context);
try {
NotificationSingleton.getInstance()
.addAllNotifcations(notifications);
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
} else {
Log.d(TAG, "No Set userId");
}
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
return null;
}
}.execute(param);
}
private GoogleAccountCredential getCredential(Context context) {
GoogleAccountCredential credential = ((ZeppaApplication) context.getApplicationContext())
.getGoogleAccountCredential();
if (credential == null) {
SharedPreferences prefs = context.getSharedPreferences(
Constants.SHARED_PREFS, Context.MODE_PRIVATE);
String email = prefs.getString(Constants.EMAIL_ADDRESS, null);
if (email != null && !email.isEmpty() && Constants.IS_CONNECTED) {
credential = GoogleAccountCredential.usingAudience(context,
Constants.APP_ENGINE_AUDIENCE_CODE);
credential.setSelectedAccountName(email);
return credential;
}
return null;
} else {
return credential;
}
}
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
private void sendNotificationsForResult(List<ZeppaNotification> resultList,
Context context) {
Log.d(TAG, "trying to send Notifications for result");
Notification.Builder notifBuilder = new Notification.Builder(context);
if (resultList.size() > 1) {
notifBuilder.setContentTitle(resultList.size()
+ " new notifications");
StringBuilder stringBuilder = new StringBuilder();
for (ZeppaNotification notification : resultList) {
stringBuilder.append(notification.getExtraMessage()).append(
'\n');
}
notifBuilder.setContentText(stringBuilder.toString());
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(Constants.INTENT_NOTIFICATIONS, true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
notifBuilder.setContentIntent(pendingIntent);
} else {
ZeppaNotification notification = resultList.get(0);
manageSingleNotification(context, notification, notifBuilder);
notifBuilder.setContentText(notification.getExtraMessage());
}
notifBuilder.setLights(Color.CYAN, 750, 3000);
notifBuilder.setAutoCancel(true);
notifBuilder.setSmallIcon(R.drawable.notif_ic_zeppa);
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
notifBuilder.setPriority(Notification.PRIORITY_DEFAULT);
notification = notifBuilder.build();
} else {
notification = notifBuilder.getNotification();
}
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Log.d(TAG, "Notification Should Post");
notificationManager.notify(0, notification);
}
private void manageSingleNotification(Context context,
ZeppaNotification notification, Notification.Builder builder) {
Intent intent = null;
switch (notification.getNotificationOrdinal()) {
case 0:
builder.setContentTitle("New Friend Request");
intent = new Intent(context, NewFriendsActivity.class);
break;
case 1:
builder.setContentTitle("New Connection");
intent = new Intent(context, UserActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_USER_ID,
notification.getFromUserId());
break;
case 2:
builder.setContentTitle("Event Recommendation");
intent = new Intent(context, EventViewActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_EVENT_ID,
notification.getEventId());
break;
case 3:
builder.setContentTitle("New Invite");
intent = new Intent(context, EventViewActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_EVENT_ID,
notification.getEventId());
break;
case 4:
builder.setContentTitle("Event Comment");
intent = new Intent(context, EventViewActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_EVENT_ID,
notification.getEventId());
break;
case 5:
builder.setContentTitle("Event Canceled");
intent = new Intent(context, MainActivity.class);
intent.putExtra(Constants.INTENT_NOTIFICATIONS, false);
break;
case 6:
builder.setContentTitle("Event Updated");
intent = new Intent(context, EventViewActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_EVENT_ID,
notification.getEventId());
break;
case 7:
builder.setContentTitle("Friend Joined Event");
intent = new Intent(context, EventViewActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_EVENT_ID,
notification.getEventId());
break;
case 8:
builder.setContentTitle("Friend Left Event");
intent = new Intent(context, EventViewActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_EVENT_ID,
notification.getEventId());
break;
case 9:
builder.setContentTitle("Let's Find a Time?");
break;
case 10:
builder.setContentTitle("Time Found!");
break;
case 11:
builder.setContentTitle("Event Reposted");
intent = new Intent(context, EventViewActivity.class);
intent.putExtra(Constants.INTENT_ZEPPA_EVENT_ID,
notification.getEventId());
break;
default: // this shouldnt happen
builder.setContentTitle("New Zeppa Notification");
intent = new Intent(context, MainActivity.class);
intent.putExtra(Constants.INTENT_NOTIFICATIONS, false);
break;
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
builder.setContentIntent(pendingIntent);
}
}
And then my Manifest needed:
<permission
android:name="package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="package.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
android:name="package.ZeppaGCMReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="package.GCMIntentService" />
</intent-filter>
</receiver>
<service
android:name="package.ZeppaGCMService"
android:enabled="true" />
Then, finally, from my endpoints class, when certain things were entered into the database, I determined which users should be notified, and fired this method:
NOTE: getDevices only returns the android device IDS. I got rid of the DeviceInfo class, it didn't seem useful but that should be the improper way of handling this. iOS is handled differently as I am sure you are aware. Will update if I change this/ if people want to see the iOS implementation.
public void sendNotificationPingToUsers(
#Named("userIds") List<Long> userIds,
ZeppaNotification notification) {
Sender sender = new Sender(Constants.SENDER_ID);
PersistenceManager mgr = getPersistenceManager();
try {
List<String> allDevices = new ArrayList<String>();
for (int i = 0; i < userIds.size(); i++) {
long userId = userIds.get(i);
ZeppaUser zeppaUser = mgr
.getObjectById(ZeppaUser.class, userId);
if (zeppaUser != null) {
ZeppaNotification specificNotification = new ZeppaNotification();
specificNotification.setToUserId(userId);
specificNotification.setFromUserId(notification
.getFromUserId());
specificNotification.setEventId(notification.getEventId());
specificNotification.setExtraMessage(notification
.getExtraMessage());
specificNotification.setNotificationType(notification
.getType());
mgr.makePersistent(specificNotification);
allDevices.addAll(zeppaUser.getDevices());
}
}
if (!allDevices.isEmpty()) {
Message msg = new Message.Builder().collapseKey("sendToSync")
.build();
MulticastResult result = sender.send(msg, allDevices, 500);
result.getTotal();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
mgr.close();
}
}
I am using oauth-signpost as my OAuth library. When I direct the user to the authorization page, the user logs in and can authorize my application. When that happens, the application is returned to focus and starts the onCreate() method instead of the onResume() method.
My code as follows:
private static final String CONSUMER_KEY = "---";
private static final String CONSUMER_SECRET = "---";
private static String ACCESS_KEY = null;
private static String ACCESS_SECRET = null;
private static final String REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
private static final String AUTH_URL = "https://api.twitter.com/oauth/authorize";
private static final String CALLBACK_URL = "myApp://Tweets";
private static CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
private static CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL,ACCESS_TOKEN_URL,AUTH_URL);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String authUrl = null;
try {
authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
} catch(OAuthMessageSignerException e) {
e.printStackTrace();
} catch(OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch(OAuthExpectationFailedException e) {
e.printStackTrace();
} catch(OAuthCommunicationException e) {
e.printStackTrace();
}
Log.d("OAuthTwitter", "authUrl" + authUrl);
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setVisibility(View.VISIBLE);
setContentView(webview);
webview.loadUrl(authUrl);
}
#Override
public void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
Log.d("OAuthTwitter", uri.toString());
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d("OAuthTwitter", verifier);
try {
provider.retrieveAccessToken(consumer, verifier);
ACCESS_KEY = consumer.getToken();
ACCESS_SECRET = consumer.getTokenSecret();
Log.d("OAuthTwitter", ACCESS_KEY);
Log.d("OAuthTwitter", ACCESS_SECRET);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
}
Manifest.xml
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
android:name=".Tweets"
android:label="#string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action
android:name="android.intent.action.VIEW" />
<category
android:name="android.intent.category.DEFAULT" />
<category
android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="myApp"
android:host="Tweets" />
</intent-filter>
</activity>
How do I get my application to call onResume() so that I can then continue with the OAuth process?
In this case, try setting your activity launch mode to singleTask.
See the link for more information: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode