Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i want to create a android app that send device imei number to .net database.
in .net insert the imei number using entity framwork and web api.
Posted
Comments
Richard MacCutchan 22-Apr-15 4:15am    
That requires two applications and rather a lot of coding. What have you done so fare?
Member 11420908 22-Apr-15 6:10am    
i just created app that detect device imei number and battery level.
in .net i crated a simple web api which shows details from database as xml file in we browser.
++++android+++
package com.example.vetriselvanv.im;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ActionBarActivity {
TextView textView, textView3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
textView = (TextView) findViewById(R.id.textView);
String string = tm.getDeviceId();
textView.setText("IMEI NUMBER IS" + string + "fhkj");
textView3 = (TextView) this.findViewById(R.id.textView3);
btrylevel();
postData();
}


private void btrylevel() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra("level", -1);
int scale = intent.getIntExtra("scale", -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
textView3.setText("Battery Level: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

public void postData() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
textView = (TextView) findViewById(R.id.textView);
final String string = tm.getDeviceId();
textView.setText(string);
textView3 = (TextView) this.findViewById(R.id.textView3);
btrylevel();
// Create a new HttpClient and Post Header
final HttpClient httpclient = new DefaultHttpClient();
final HttpPost httppost = new HttpPost("http://localhost:50554/api/Default");
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra("level", -1);
int scale = intent.getIntExtra("scale", -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
textView3.setText("Battery Level: " + level + "%");
try {
// Add your data
List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>(3);
nameValuePairs.add(new BasicNameValuePair("Id", "5"));
nameValuePairs.add(new BasicNameValuePair("btrylevel", textView3.toString()));
nameValuePairs.add(new BasicNameValuePair("imei", string));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute H
Member 11420908 22-Apr-15 6:17am    
+++++++++.NET+++++++++++++
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApi.Controllers
{
public class DefaultController : ApiController
{
Database2Entities db=new Database2Entities();
// GET: api/Default
public IEnumerable<device> Get()
{
return db.Devices.AsEnumerable();
}

// GET: api/Default/5
public Device Get(int id)
{
return db.Devices.Where(x => x.Id == id).FirstOrDefault();
}

// POST: api/Default
public Device Post([FromBody]int id,int imei,int btrylevel)
{
Device device = new Device()
{
Id = id,
btrylevel = btrylevel,
imei = imei

};
db.Devices.Add(device);
db.SaveChanges();
return device;
}

// PUT: api/Default/5
public void Put(int id, [FromBody]string value)
{
}

// DELETE: api/Default/5
public void Delete(int id)
{
}
}
}

Since you are going to use Web API, why are you confusing yourself to use native .NET framework at all?

You should build a simple Android application with default WebView control added to it. Which can then target your API. Do you know much about ASP.NET Web API[^]? If not then please learn it there. You should also have enough understanding of routing in ASP.NET Web API; it is just a little different from ASP.NET MVC Routing.

Then, you can pass your data from Android application. In your application store all of the data that you want to send to your Web API. While creating the routing make sure you create an efficient routine scheme which would let you upload the data. On the Web API use Entity framework to commit all of the changes to your database. There is no need to involve native .NET framework's procedures, just stick to the Web API in this case.

Also there is only SQL Server database, there is no .NET database.
 
Share this answer
 
could you please send me some sample examples.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Jun-15 1:53am    
Sorry, no. This post is considered abusive as this is not a "solution". Please remove it.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900