انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
نوتیفیکیشن برای نمایش متن و عکس دریافت شده از سرور (برنامه نویسی اندروید)

نوتیفیکیشن برای نمایش متن و عکس دریافت شده از سرور (برنامه نویسی اندروید)

#2
فایل Main.java :

کد پی‌اچ‌پی:
package com.toobaweb.co;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;

public class 
Main extends Activity {

    @
SuppressWarnings("deprecation")
    @
Override
    
protected void onCreate(Bundle savedInstanceState) {
            
super.onCreate(savedInstanceState);
            
setContentView(R.layout.main);
            
            new 
NetCheck().execute();      
            
    }
    
    
    
// چک کردن کانکشن اینترنت کاربر
    
private class NetCheck extends AsyncTask<String,String,Boolean>
   {
       private 
ProgressDialog nDialog;

       @
Override
       
protected void onPreExecute(){
           
super.onPreExecute();
           
nDialog = new ProgressDialog(Main.this);
           
nDialog.setTitle("چک کردن کانکشن اینترنت");
           
nDialog.setMessage("در حال بررسی ...");
           
nDialog.setIndeterminate(false);
           
nDialog.setCancelable(true);

           
nDialog.show();
       }
       
/**
        * Gets current device state and checks for working internet connection by trying Google.
       **/
       
@Override
       
protected Boolean doInBackground(String... args){

           
ConnectivityManager cm = (ConnectivityManagergetSystemService(Context.CONNECTIVITY_SERVICE);
           
NetworkInfo netInfo cm.getActiveNetworkInfo();
           if (
netInfo != null && netInfo.isConnected()) {
               try {
                   
URL url = new URL("http://www.google.com");
                   
HttpURLConnection urlc = (HttpURLConnectionurl.openConnection();
                   
urlc.setConnectTimeout(3000);
                   
urlc.connect();
                   if (
urlc.getResponseCode() == 200) {
                       return 
true;
                   }
               } catch (
MalformedURLException e1) {
                   
// TODO Auto-generated catch block
                   
e1.printStackTrace();
               } catch (
IOException e) {
                   
// TODO Auto-generated catch block
                   
e.printStackTrace();
               }
           }
           return 
false;

       }
       @
Override
       
protected void onPostExecute(Boolean th){

           if(
th == true){
               
nDialog.dismiss();
               
Toast.makeText(getApplicationContext(), "اینترنت وصل می باشد ."Toast.LENGTH_LONG).show();
               
CreateNotification();
           }
           else{
               
nDialog.dismiss();
               
Toast.makeText(getApplicationContext(), "اینترنت قطع می باشد."Toast.LENGTH_LONG).show();
           }
       }
   }


    private 
void CreateNotification(){
        
CharSequence renderText "در حال ساخت نوتیفیکیشن جدید";
        
CharSequence title "نوتیفیکیشن";
        
CharSequence text "ساخت نوتیفیکیشن جدید";
        
long time System.currentTimeMillis();
        
Context context getApplicationContext();
        
        
Intent notifyIntent = new Intent(this Main.class);
        
PendingIntent contentIntent PendingIntent.getActivity(context0notifyIntent0);
        
        final 
Notification notification = new Notification(R.drawable.ic_launcher renderText time);
        
notification.setLatestEventInfo(contexttitletextcontentIntent);
        
        
String notyService Context.NOTIFICATION_SERVICE;
        final 
NotificationManager nManager = (NotificationManager)  getSystemService(notyService);
        
        
nManager.notify(1notification);
        
    }


تو این فایل ابتدا میاد اینترنت کاربر رو چک میکنه و اگر اینترنت کاربر وصل بود یک نوتیفیکیشن ایجاد میشه و اگر هم وصل نبود یه پیام ظاهر میشه ...
فراموش نکنید که دسترسی های زیر رو در فایل AndroidManifest.xml اضافه کنید :

کد پی‌اچ‌پی:
<uses-permission android:name="android.permission.INTERNET" />
    <
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

امیدوارم کد مورد نظر به دردتون بخوره .
موفق باشید
پاسخ
 سپاس شده توسط admin ، nassa ، djary ، sohrabjam ، farhad1793 ، شماره مجازی امارات


پیام‌های این موضوع
RE: نوتیفیکیشن - توسط Ali Majidi - ۱۳۹۴/۰۶/۲۵, ۱۲:۳۴ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان