unable to instantiate activity - java

Probably it is just a silly mistake but i am having a Instantiate activity message when i tried to run my second activity
My first activity :
package com.burak.algonder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b= (Button) findViewById(R.id.but2);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(getBaseContext(), "opucuk!", Toast.LENGTH_LONG);
toast.show();
startActivity(new Intent(MainActivity.this, Pozsyon.class));
}
});
}
}
My second Activity:
package com.burak.algonder;
import java.util.List;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class Pozsyon extends Service implements LocationListener {
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(
Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
My Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.burak.algonder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.burak.algonder.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.burak.algonder.Pozsyon" >
<intent-filter>
<action android:name="android.intent.action.Pozsyon" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<receiver android:name="com.burak.algonder.SmsReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Thank you for your help.

Pozsyon Class is not Activity and can't be started here!

Related

Try to the File.listFiles() is returning null in android 11

I was creating an application to test whether File.listFiles() method is working or not. To check this I made an application and I used it there but this returning null in place of an array. This is my full code please help and I have granted all permissions for android 11
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
tools:node="merge" />
<uses-permission
android:name="android.permission.STORAGE"
tools:node="merge" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="#style/Theme.MyApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
public class MainActivity extends AppCompatActivity {
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
deleteButton = findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
deleteFiles(view);
}
});
}
private void deleteFiles(View v) {
File file = new File("/storage/emulated/0/DCIM/");
if (file.isDirectory())
{
File[] files;
files = file.listFiles();
int a = files.length;
for (int i = 0; i < files.length; i++)
{
new File(file, files[i].getName()).delete();
}
Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), (CharSequence) file, Toast.LENGTH_LONG).show();
}
}
}

How to start android app as service after booting

I am trying to start up an android app as background service after initial run. At first run, the app should start as normal android app, then after, that it should be a background service which still runs even after booting. I wrote the following code. The service class is verified as standalone android app. But this app is not running as expected. No error code, but when debug, service class is not working.
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tulga.nar.mytrack">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Initial one time running activity class:
package com.tulga.nar.mytrack;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
}
public void starterHandler(View v)
{
new MyBroadcastReceiver();
}
}
Initial one time running activity class's xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tulga.nar.mytrack.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/starter"
android:text="start service"
android:onClick="starterHandler"/>
</android.support.constraint.ConstraintLayout>
Broadcast receiver class. It is called from MainActivity after start service button is clicked. It is supposed to start background MyService class.
package com.tulga.nar.mytrack;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context aContext, Intent aIntent) {
// This is where you start your service
aContext.startService(new Intent(aContext, MyService.class));
}
}
This is background MyService class. The class is verified and works as standalone app. But here, code execution is not reaching to here when debug.
package com.tulga.nar.mytrack;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by Home on 8/27/2017.
*/
public class MyService extends AppCompatActivity implements
LocationListener
{
private class SendDeviceDetails extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String data = "";
HttpURLConnection connection = null;
try {
URL url=new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type",
"application/json");
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();
DataOutputStream wr = new
DataOutputStream(connection.getOutputStream());
wr.writeBytes(params[1]);
wr.flush();
wr.close();
InputStream in = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} //catch (Exception e) {
//e.printStackTrace();
//}
catch (MalformedURLException e) {
e.printStackTrace();}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (connection != null) {
connection.disconnect();
}
}
return data;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("TAG", result); // this is expecting a response code to be
sent from your server upon receiving the POST data
}
}
public static final int RequestPermissionCode = 1 ;
static public double myLong=151;
static public double myLat=-34;
static public String _id=null;
static public String _rev=null;
Button buttonEnable, buttonGet ;
TextView textViewLongitude, textViewLatitude ;
Context context;
Intent intent1 ;
Location location;
LocationManager locationManager ;
boolean GpsStatus = false ;
Criteria criteria ;
String Holder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
EnableRuntimePermission();
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
Holder = locationManager.getBestProvider(criteria, false);
context = getApplicationContext();
CheckGpsStatus();
JSONObject postData = new JSONObject();
try {
postData.put("_id",String.valueOf("9876543210"));
postData.put("Lat",
String.valueOf(String.valueOf(MyService.myLat)));
postData.put("Long",
String.valueOf(String.valueOf(MyService.myLong)));
postData.put("_rev",String.valueOf("1-
62076042d87cacd2711268d4a396129b"));
new SendDeviceDetails().execute("my-server-name/mydatabase",
postData.toString());
}
catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onLocationChanged(Location location) {
textViewLongitude.setText("Longitude:" + location.getLongitude());
textViewLatitude.setText("Latitude:" + location.getLatitude());
myLong=location.getLongitude();
myLat=location.getLatitude();
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
public void CheckGpsStatus(){
locationManager =
(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
GpsStatus =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
public void EnableRuntimePermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(MyService.this,
Manifest.permission.ACCESS_FINE_LOCATION))
{
Toast.makeText(MyService.this,"ACCESS_FINE_LOCATION permission
allows us to Access GPS in app", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MyService.this,new String[]{
Manifest.permission.ACCESS_FINE_LOCATION},
RequestPermissionCode);
}
}
#Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult)
{
switch (RC) {
case RequestPermissionCode:
if (PResult.length > 0 && PResult[0] ==
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MyService.this,"Permission Granted, Now your
application can access GPS.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MyService.this,"Permission Canceled, Now your
application cannot access GPS.", Toast.LENGTH_LONG).show();
}
break;
}
}
You didn't regest your serice in mainifest,and your broadcastReceiver is not at the right place.It should like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tulga.nar.mytrack">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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" android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".service.MyService">
<intent-filter>
<action android:name="write your action name" />
</intent-filter>
</service>
</application>
</manifest>

WatchFaces are not configuring

I have created watchface for android wear and my code is as follows:
ComapnionConfigActivity (in mobile module):
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Wearable;
public class CompanionConfigActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_companion_config);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
#Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_watch_config, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
Manifest for mobile module:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agile.mywatchface" >
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/square"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".CompanionConfigActivity"
android:label="#string/title_activity_watch_config" >
<intent-filter>
<action android:name="com.example.agile.mywatchface.MAIN" />
<category android:name= "com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MyWatchfaceService: (service in wear module for drawing watchface)
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.wearable.watchface.CanvasWatchFaceService;
import android.view.SurfaceHolder;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* Created by agile on 3/5/2015.
*/
public class MyWatchfaceService extends CanvasWatchFaceService{
Calendar c;
int hrs,min,sec,width,height;
Timer timer;
TimerTask timerTask;
Paint paint;
#Override
public Engine onCreateEngine() {
return new MyEngine();
}
class MyEngine extends CanvasWatchFaceService.Engine {
#Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);
timer = new Timer();
timerTask = new MyTimerTask();
c = Calendar.getInstance();
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(18);
}
#Override
public void onPropertiesChanged(Bundle properties) {
super.onPropertiesChanged(properties);
}
#Override
public void onTimeTick() {
super.onTimeTick();
}
#Override
public void onDraw(Canvas canvas, Rect bounds) {
updateTimeOnEachSecond();
width = bounds.width();
height = bounds.height();
canvas.drawText(String.valueOf(hrs)+":"+String.valueOf(min)+":"+String.valueOf(sec),width/2,height/2,paint);
}
#Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
}
}
public void updateTimeOnEachSecond() {
timer.scheduleAtFixedRate(timerTask, 0, 1000);
}
class MyTimerTask extends TimerTask {
public void run(){
hrs = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
sec = c.get(Calendar.SECOND);
}
}
}
Manifest for wear module:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.agile.mywatchface" >
<uses-feature android:name="android.hardware.type.watch" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/square"
android:label="#string/app_name"
>
<service
android:name=".MyWatchfaceService"
android:label="#string/app_name"
android:permission="android.permission.BIND_WALLPAPER"
android:allowEmbedded="true"
>
<!-- companion configuration activity -->
<meta-data
android:name="android.service.wallpaper"
android:resource="#xml/watch_face" />
<meta-data
android:name="com.example.agile.mywatchface.preview"
android:resource="#drawable/square" />
<meta-data
android:name="com.example.agile.mywatchface.preview_circular"
android:resource="#drawable/square" />
<meta-data
android:name="com.google.android.wearable.watchface.companionConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
<!-- wearable configuration activity -->
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
<category android:name="com.example.agile.mywatchface.category.WATCH_FACE" />
</intent-filter>
</service>
<activity
android:name=".WearConfigActivity"
android:label="#string/title_activity_wear_config"
android:allowEmbedded="true">
<intent-filter>
<action android:name="com.example.agile.mywatchface.CONFIG_DIGITAL" />
<category android:name=
"com.google.android.wearable.watchface.category.WEARABLE_CONFIGURATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="com.google.android.wearable.watchface.wearableConfigurationAction"
android:value="com.example.agile.mywatchface.MAIN" />
</activity>
</application>
</manifest>
I have followed official guide for creating watchface. But, when I install the app, it isn't showing in companion app. It seems like the problem is in manifest or companionconfigactivity. I tried for more than a day but couldn't identify it. Can anyone help me out?
In your Wearable Manifest you have following line:
<category android:name="com.example.agile.mywatchface.category.WATCH_FACE" />
This is incorrect, you need to use
<category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
Android Wear looks for services with this category in the filter, it will ignore yours if you use custom category.

android - exception when trying to open the front camera

i am unable to get the the front facing camera (camera id 1). open cam #1 failed appears on screen when i try to run the following code.
enclosed a snippet. any help will be appreciated (i am trying it on nexus 4).
Tester.java
package com.example.cam;
import java.util.HashMap;
import java.util.Iterator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
public class Tester extends Activity {
private static final int[] camIds = { Camera.CameraInfo.CAMERA_FACING_BACK, Camera.CameraInfo.CAMERA_FACING_FRONT };
#SuppressLint("UseSparseArrays")
private final HashMap<Integer, Camera> camMap = new HashMap<Integer, Camera>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
protected void onResume() {
super.onResume();
if (!checkCameraHardware())
finish();
getCameras();
releaseCamera();
}
#Override
protected void onPause() {
super.onPause();
releaseCamera();
}
#Override
protected void onStop() {
super.onStop();
releaseCamera();
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseCamera();
}
private final void doToast(final String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
private final void getCameras() {
for (int camId : camIds)
try {
camMap.put(camId, Camera.open(camId));
doToast("open cam #" + camId + " succeeded");
} catch (Exception e) {
doToast("open cam #" + camId + " failed");
}
}
private final void releaseCamera() {
final Iterator<Integer> iter = camMap.keySet().iterator();
while (iter.hasNext())
camMap.remove(iter.next()).release();
}
private final boolean checkCameraHardware() {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
return camIds.length <= Camera.getNumberOfCameras();
else
return false;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cam"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.cam.Tester"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Fortumo In-App payment for Android

I'm trying to create a application, where you can buy In-App credits with the Fortumo service. I've managed to get the makePayment(); method working so I can make a payment and everything seems to work. The only thing is, that it looks like the onReceive(); is never called because the credits count is not changed after a successful payment. The credits count should be increased by 32000 with one payment of 0.32€.
Here is the main activity java:
package com.your.raha;
import com.your.raha.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class Soundboard extends Activity {
private SoundManager mSoundManager;
static int counter;
static TextView display;
SharedPreferences someData;
public static String filename = "rahafail";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.proov);
display = (TextView) findViewById(R.id.tvKonto);
someData = getSharedPreferences(filename, 0);
int returnedInt = someData.getInt("sharedInt", 0);
counter = returnedInt;
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound1);
Button SoundButton1 = (Button)findViewById(R.id.sound1);
SoundButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
AddOne();
}
});
Button bOsta = (Button)findViewById(R.id.bOsta);
bOsta.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onStop();
startActivity(new Intent("com.uugudev.rahaboss.FORTUMOMAKSA"));
}
});
ImageButton Abi = (ImageButton)findViewById(R.id.ibAbi);
Abi.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
counter = 0;
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
display.setText(counter + " EEK");
}
});
}
public static void AddOne() {
// TODO Auto-generated method stub
counter++;
display.setText(counter + " EEK");
}
public static void Osteti() {
// TODO Auto-generated method stub
counter = counter + 32000;
display.setText(counter + " EEK");
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
int returnedInt = someData.getInt("sharedInt", 0);
counter = returnedInt;
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
SharedPreferences.Editor editor = someData.edit();
editor.putInt("sharedInt", counter);
editor.commit();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
display.setText(counter + " EEK");
}
}
The payment activity class:
package com.your.raha;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
import com.fortumo.android.Fortumo;
import com.fortumo.android.PaymentActivity;
import com.fortumo.android.PaymentRequestBuilder;
import com.fortumo.android.PaymentResponse;
public class FortumoMaksa extends PaymentActivity {
/** Called when the activity is first created. */
public int raha;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Fortumo.enablePaymentBroadcast(this, Manifest.permission.PAYMENT_BROADCAST_PERMISSION);
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setDisplayString("Donate");
builder.setProductName("stars_myawesomeusername");
makePayment(builder.build());
}
;
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onPaymentCanceled(PaymentResponse response) {
Toast.makeText(this, "Kasutaja katkestas makse", Toast.LENGTH_SHORT).show();
finish();
}
#Override
protected void onPaymentFailed(PaymentResponse response) {
Toast.makeText(this, "Makse ebaõnnestus", Toast.LENGTH_SHORT).show();
finish();
}
#Override
protected void onPaymentPending(PaymentResponse response) {
Toast.makeText(this, "Makse ootel", Toast.LENGTH_SHORT).show();
finish();
}
#Override
protected void onPaymentSuccess(PaymentResponse response) {
Toast.makeText(this, "Makse õnnestus", Toast.LENGTH_SHORT).show();
finish();
}
}
The PaymentStatusReceived class:
package com.your.raha;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.fortumo.android.Fortumo;
public class PaymentStatusReceiver extends BroadcastReceiver {
private static String TAG = "PaymentStatusReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
int billingStatus = extras.getInt("billing_status");
if(billingStatus == Fortumo.MESSAGE_STATUS_BILLED) {
Soundboard.Osteti();
}
}
}
The AndroidManifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.your.raha"
android:installLocation="auto"
android:versionCode="2"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<permission
android:name="com.your.domain.PAYMENT_BROADCAST_PERMISSION"
android:label="Read Fortumo payment status"
android:protectionLevel="signature" />
<uses-permission android:name="com.your.raha.PAYMENT_BROADCAST_PERMISSION" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name="com.fortumo.android.BillingSMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<service android:name="com.fortumo.android.FortumoService" />
<service android:name="com.fortumo.android.StatusUpdateService" />
<activity
android:name="com.fortumo.android.FortumoActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<receiver
android:name=".PaymentStatusReceiver"
android:permission="com.your.raha.PAYMENT_BROADCAST_PERMISSION" >
<intent-filter>
<action android:name="com.fortumo.android.PAYMENT_STATUS_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.fortumo.android.FortumoActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".PaymentStatusReceiver"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.uugudev.rahaboss.PAYMENTSTATUSRECEIVER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<activity
android:name="com.fortumo.android.FortumoActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<service android:name="com.fortumo.android.FortumoService" />
<activity
android:name=".Soundboard"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".FortumoMaksa"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.uugudev.rahaboss.FORTUMOMAKSA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
So basically I'm trying to start the Soundboard.Osteti(); in the Soundboard.java that adds 32000 to the counter int and then updates the TextView. But nothing happens.
Hope somebody can help me. Thanks!
I'm still trying to make this work.
There is this thing I think you've wrong in your manifest:
PaymentStatusReceiver it's not an activity, and you only need to configure that as a receveiver (as you already had).

Categories